openmpcppyy

Problem creating OpenMP enabled PCH programmatically with cppyy


I've been trying to generate a OpenMP enabled precompiled header (PCH) with cppyy and have been failing so far. Instead of doing so manually each time as outlined in this answer, I'm looking for an automated solution.

So far, the broad method is to use os.environ to export environmental variables - but this seems to fail somewhat inconsistently, resulting in single-threaded code each time.

In particular, there doesn't seem to be any output from cppyy_backend.loader.ensure_precompiled_header all the time. Even if there is output, it has no effect on whether the C++ code runs parallely or not.

The relevant part of the code below:

import cppyy_backend.loader as l 

os.environ['EXTRA_CLING_ARGS'] = '-fopenmp -O2 -g'
l.set_cling_compile_options(True)
current_folder = os.path.split(os.path.abspath(__file__))[0]
pch_folder = os.path.join(current_folder, 'cling_pch/')
if os.path.exists(pch_folder):
    pass
else:
    os.mkdir(pch_folder)

l.ensure_precompiled_header(pch_folder)
    
# find PCH file:
pch_path = glob.glob(pch_folder+'/allDict*')
if len(pch_path)>0:
    os.environ['CLING_STANDARD_PCH'] = pch_path[0]
else:
    raise ValueError('Unable to find a precompiled header...')

System specs:

EDIT: I made sure the custom PCH folder was first empty while checking to see if the custom PCH file was being created.


Solution

  • If you set CLING_STANDARD_PCH to the desired name, it will be used as the name for the generated PCH. Conversely, if you set the folder name to the lower level call from loader, it will set CLING_STANDARD_PCH for you. Setting both should not be necessary.

    The default naming scheme will generate a differently named PCH if OpenMP is enabled from when it's not (a .omp. part is added to the name). This allows you to go back end forth, without bothering having to rebuild the PCH each time. Likely, you have multiple editions in the cling_pch directory, and pch_path[0] may well be a non-OpenMP variant.

    If you want to stay close to the above, though, then how about:

    import os
      
    os.environ['EXTRA_CLING_ARGS'] = '-fopenmp -O2 -g'
    current_folder = os.path.split(os.path.abspath(__file__))[0]
    pch_folder = os.path.join(current_folder, 'cling_pch')
    if os.path.exists(pch_folder):
        pass
    else:
        os.mkdir(pch_folder)
    
    os.environ['CLING_STANDARD_PCH'] = os.path.join(pch_folder, 'std_with_openmp.pch')
    
    import cppyy
    
    cppyy.cppexec("std::cerr << _OPENMP << std::endl")