Can OpenMP worker-threads be named?

Viewed 36

Using top, I can see my application working -- and consuming multiple CPUs, as expected. However, I'd like to be able to drill down into the CPU-consumption by the different workers, and yet, these are indiscernible in top's output, which shows the same name for all of them: myprogram{myprogram}.

Is there an OpenMP clause allowing to name each worker? If not, perhaps some other tricks -- using the pthread_setname_np(), perhaps -- which will work on all (or most) platforms?

Update: I tried using the pthread_setname_np() and am observing unexpected results: multiple threads running with the same name... My program uses nested parallelization:

  1. The #pragma omp parallel for is calling a function to upload a file -- multiple such uploads are running in parallel.
  2. The upload-function loops over the given file, reading a block at a time. Each block is then:
  • fed to SHA256_Update()
  • fed to sftp_write()

The idea is to compute each file's digest, while the file is passing through the RAM anyway. The digest-update and the writing are done in parallel:

#pragma omp parallel sections
{
#pragma omp section
    {
        char tname[32];
        sprintf(tname, sizeof tname, "SHA %d", omp_get_thread_num());
        pthread_setname_np(pthread_self(), tname));
        SHA256_Update(&ctx, buf);
        pthread_setname_np(pthread_self(), ""));
    }
#pragma omp section
    {
        char tname[32];
        sprintf(tname, sizeof tname, "SFTP %d", omp_get_thread_num());
        pthread_setname_np(pthread_self(), tname));
        written = sftp_write(sftp, buf, bufsize);
        pthread_setname_np(pthread_self(), ""));
    }
}

Bizarrely, what I see in top -- and in gdb -- are several threads named "SFTP 1". Sometimes a name "SHA 1" flashes by, but never any other number, even though I see 7 files being uploaded in parallel...

1 Answers

There is no standard way to set a thread name in OpenMP (assuming I read the specification correctly). You can check that in the specification: https://www.openmp.org/wp-content/uploads/OpenMP-API-Specification-5-2.pdf .

If your OpenMP runtime use pthread (typically on Linux and BSD but certainly not on Windows), then you can use the function pthread_self to get the thread context in a parallel section. Then you can use the function pthread_setname_np. However, please not that the argument of the function change regarding the OS used. See How to set the name of a thread in Linux pthreads? . Put it shortly, there is no portable solution.

Related