1

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...

Mikhail T.
  • 3,043
  • 3
  • 29
  • 46

1 Answers1

3

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.

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59
  • Thanks. Any comment on my example -- why am I seeing multiple threads with the same names, even though I reset the names back to "" before exiting the parallel sections? – Mikhail T. Jun 22 '22 at 15:56
  • You should check the return code of the function as it can fail for example if the name is too big. Additionally, the function can only work assuming OpenMP threads on your platform are pthreads ones. This is certainly not the case on Windows AFAIK for most runtimes. Besides, I am not sure `pthread_setname_np(pthread_self(), ""));` is actually valid (though it is not explicitly prohibited). Finally, the tool used to check the name might be unreliable. You can try to use a debugger or even `pthread_getname_np` to be sure (and check the return value too). – Jérôme Richard Jun 23 '22 at 18:24
  • I'm developing on FreeBSD, where it is pthread. I checked the names in gdb -- having stopped the process entirely -- and saw multiple threads sharing the _same_ name, which baffles me. Maybe, wiping out the name with `""` is not working, indeed... Khmm... – Mikhail T. Jun 23 '22 at 19:34