Search code examples
clangopenmpllvmllvm-clang

How to print something to the console from OpenMP source Code?


I am modifying OpenMP source Code, and I want to make sure that it is indeed working. For example the following code:

#include "omp.h"

int main()
{
    int i=0;
    #pragma omp parallel for schedule(dynamic,4)
    for(i=0;i<1000;++i)
    {
        int x = 4+i;
    }
}

Should call __kmpc_dispatch_init_4() And I have verified this by using emit-llvm option in clang.

To double check I added the following print statement:

void __kmpc_dispatch_init_4(ident_t *loc, kmp_int32 gtid,
                            enum sched_type schedule, kmp_int32 lb,
                            kmp_int32 ub, kmp_int32 st, kmp_int32 chunk) {
  KMP_DEBUG_ASSERT(__kmp_init_serial);
  printf("%s\n", "Hello OpenMP");

#if OMPT_SUPPORT && OMPT_OPTIONAL
  OMPT_STORE_RETURN_ADDRESS(gtid);
#endif
  __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk, true);
}

But when I am compiling the code, I am not getting this output on terminal.

I am compiling the code like this:

llvm_build/bin/clang sc.c  -L/usr/local/lib -fopenmp

And after building the openmp source code the output of sudo make install is this:

Install the project...
-- Install configuration: "Release"
-- Up-to-date: /usr/local/lib/libomp.so
-- Up-to-date: /usr/local/include/omp.h
-- Up-to-date: /usr/local/include/omp-tools.h
-- Up-to-date: /usr/local/include/ompt.h
-- Up-to-date: /usr/local/lib/libomptarget.so
-- Up-to-date: /usr/local/lib/libomptarget.rtl.x86_64.so

To cross check if this is used I used:

ldd a.out

And the output is:

linux-vdso.so.1 (0x00007fff25bb6000)
    libomp.so => /usr/local/lib/libomp.so (0x00007f75a52c6000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f75a50a7000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f75a4cb6000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f75a4ab2000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f75a558a000)

So I am assuming it should be using the code that I have modified. But not able to see the output of the print statement. I have also tried using fprintf(stderr, ....) But doesn't work.

Thank you in advance.


Solution

  • First, you should flush the output using fflush(stdout) after the printf to enforce the line to be printed in the console. Moreover, you should also ensure that /usr/local/lib/libomp.so is the good one by just checking the modification date of the file (with the stat command). If the loaded file is not the good one you can force it with the LD_LIBRARY_PATH, LIBRARY_PATH and LD_PRELOAD environment variables. If this is not sufficient, you can use the nm or objdump tools to check the dynamic symbols: the function should be undefined in your program and provided by the modified LLVM OpenMP runtime.

    PS: If none of this works, I strongly advise you to build your program and the OpenMP runtime library with debugging (and tracing) information so you can use debuggers like gdb in order to track function calls.