madgraph5 / madgraph4gpu

GPU development for the Madgraph5_aMC@NLO event generator software package
30 stars 33 forks source link

cudacpp_backend card should be removed from output.py (it is only needed in launch_plugin.py) #1015

Open valassi opened 2 days ago

valassi commented 2 days ago

We should understand if cudacpp_backend card should be defined only in output.py or launch_plugin.py

This is a followup to #1009 (which was closed because it included also other things, now fixed by Olivier)

valassi commented 2 days ago

A bit of reverse engineering and debugging

(1) About output.py

In output.py cudacpp_backend is in function add_input_for_banner

If the call to self.add_input_for_banner() is commented out, then the file gg_tt.mad/bin/internal/plugin_run_card is not generated. Is this needed? I will check later.

(2) About launch_plugin.py

This section

template_on = \
"""#***********************************************************************
# SIMD/GPU configuration for the CUDACPP plugin
#************************************************************************
# %(cudacpp_backend)s = cudacpp_backend ! CUDACPP backend: fortran, cuda, hip, cpp, cppnone, cppsse4, cppavx2, cpp512y, cpp512z, cppauto

is appended at the end of the run_card.dat by this code

https://github.com/mg5amcnlo/mg5amcnlo/blob/55a291dc8def762938a964cc1343c6d76af5b843/madgraph/various/banner.py#L3017

            if not self.list_parameter:
                text = text % self
            else:
                data = dict((key.lower(),value) for key, value in self.items())              
                for name in self.list_parameter:
                    if self.list_parameter[name] != str:
                        data[name] = ', '.join(str(v) for v in data[name])
                    else:
                        data[name] = "['%s']" % "', '".join(str(v) for v in data[name])
                text = text % data

Line 3017 is text%data. This is where the %(cudacpp_backend)s is replaced.

If this part is commented out, then this fails

    def default_setup(self):
        super().default_setup()
        cudacpp_supported_backends = [ 'fortran', 'cuda', 'hip', 'cpp', 'cppnone', 'cppsse4', 'cppavx2', 'cpp512y', 'cpp512z', 'cppauto' ]
        #self.add_param('cudacpp_backend', 'cpp',
        #               include=False, # AV: 'include=True' would add "CUDACPP_BACKEND = 'cpp'" to run_card.inc
        #               hidden=False, # AV: keep cudacpp_backend in runcard template and keep 'hidden='False'
        #               allowed=cudacpp_supported_backends)

This (the fact that it fails) happens also if plugin_run_card exists (if I checked well).

valassi commented 2 days ago

Ok now I understand it.

In cudacpp launch_plugin.py

class CPPRunCard(banner_mod.RunCardLO):
...
    def plugin_input(self, finput):
        return

In mg5amcnlo banner.py

class ConfigFile(dict):
    ...
    def __init__(self, finput=None, **opt):
        ...
        self.plugin_input(finput)        
    ...
    def plugin_input(self, finput=None):
        pass
...
class RunCard(ConfigFile):
   ...
    def plugin_input(self, finput):
        if not finput and not MADEVENT:
            return
        curr_dir = None
        if isinstance(finput, file):
            # expected path to be like "XXXX/Cards/run_card.dat"
            curr_dir = os.path.dirname(os.path.dirname(finput.name))
        elif isinstance(finput, str):
            curr_dir = os.path.dirname(os.path.dirname(finput))
        if curr_dir:
            if os.path.exists(pjoin(curr_dir, 'bin', 'internal', 'plugin_run_card')):
                # expected format {} passing everything as optional argument
                for line in open(pjoin(curr_dir, 'bin', 'internal', 'plugin_run_card')):
                    if line.startswith('#'):
                        continue
                    opts = dict(eval(line))
                    self.add_param(**opts)

In short

I will make a small PR for this (I wanted this now because this is in the way of the multi bcakend PR 'grid' in #948,m which I want for CHEP)