|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using ApplicationLauncher; |
| 5 | +using ApplicationConfiguration; |
| 6 | + |
| 7 | +namespace Application |
| 8 | +{ |
| 9 | + public interface ILoader { |
| 10 | + ICollection<IAction> Load (); |
| 11 | + bool EnableParallelMode { get; } |
| 12 | + } |
| 13 | + |
| 14 | + public enum ActionSequence { Continue, Stop, Loop, Abort }; |
| 15 | + |
| 16 | + public interface IAction |
| 17 | + { |
| 18 | + void Setup(ref Mono.Options.OptionSet opt_set); |
| 19 | + bool Configure(); // return true on error while configuring |
| 20 | + ActionSequence Run(string parameters); |
| 21 | + string Name { get; } |
| 22 | + } |
| 23 | + |
| 24 | + public class Application : IAction |
| 25 | + { |
| 26 | + #region CONFIGURATIONS |
| 27 | + private string APPLICATION_BINARY = null; |
| 28 | + private ApplicationConfiguration.ApplicationConfiguration configuration; |
| 29 | + #endregion |
| 30 | + |
| 31 | + public Application(ApplicationConfiguration.ApplicationConfiguration configuration) |
| 32 | + { |
| 33 | + this.configuration = configuration; |
| 34 | + // Disables that default variable (may be confusing for sequential run) |
| 35 | + Environment.SetEnvironmentVariable ("ARCANE_PARALLEL_SERVICE", null); |
| 36 | + } |
| 37 | + |
| 38 | + public virtual void Setup(ref Mono.Options.OptionSet opt_set) |
| 39 | + { |
| 40 | + Env.CreateTag ("Run", "Default execution environment"); |
| 41 | + Env.CreateTag ("Simulation", "Simulation execution environment"); |
| 42 | + } |
| 43 | + |
| 44 | + public virtual bool Configure() |
| 45 | + { |
| 46 | + bool has_error = false; |
| 47 | + APPLICATION_BINARY = Path.Combine(Launcher.Options.binaryDir, configuration.Name + ".exe"); |
| 48 | + if (!File.Exists (APPLICATION_BINARY)) { |
| 49 | + Logger.log(Logger.Channel.Error, "Cannot access Application executable in '{0}'", APPLICATION_BINARY); |
| 50 | + has_error = true; |
| 51 | + } else if (Launcher.Options.check_mode) { |
| 52 | + Logger.log(Logger.Channel.Info,"Application found at location : " + APPLICATION_BINARY); |
| 53 | + } |
| 54 | + |
| 55 | + if (!ExecTools.isWindows () && Launcher.Options.libDir != null) { |
| 56 | + string LD_LIBRARY_PATH = Launcher.Options.libDir; |
| 57 | + Env.Export (Env.GetTag("Run"), "LD_LIBRARY_PATH", LD_LIBRARY_PATH); |
| 58 | + } |
| 59 | + |
| 60 | + if (configuration.ConfigLocation == ConfigLocation.ApplicationSide) { |
| 61 | + string STDENV_PATH_SHR = Launcher.Options.binaryDir; |
| 62 | + string STDENV_PATH_SHR_FILE = Path.Combine (STDENV_PATH_SHR, String.Format ("{0}.config", configuration.Name)); |
| 63 | + if (File.Exists (STDENV_PATH_SHR_FILE)) { |
| 64 | + Env.Export (Env.GetTag ("Run"), "STDENV_PATH_SHR", STDENV_PATH_SHR); |
| 65 | + } else if (Launcher.Options.check_mode) { |
| 66 | + Logger.log (Logger.Channel.Error, "Standard config file '{0}' cannot be found", STDENV_PATH_SHR_FILE); |
| 67 | + has_error = true; |
| 68 | + } else { |
| 69 | + Logger.log (Logger.Channel.Warning, "Standard config file '{0}' cannot be found", STDENV_PATH_SHR_FILE); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + has_error |= configureVerboseRun (); |
| 74 | + return has_error; |
| 75 | + } |
| 76 | + |
| 77 | + public virtual ActionSequence Run(string parameters) |
| 78 | + { |
| 79 | + Logger.log (Logger.Channel.Banner, "Simulation mode"); |
| 80 | + int? nproc = null; |
| 81 | + if (configuration.EnableParallelMode) { |
| 82 | + nproc = Launcher.Options.nb_proc; |
| 83 | + } else { |
| 84 | + if (Launcher.Options.nb_proc > 1) |
| 85 | + throw new ApplicationLauncher.InternalException ("Illegal #proc on non-parallel application"); |
| 86 | + } |
| 87 | + int res = runApplication (nproc, parameters, Env.GetTag ("Simulation")); |
| 88 | + |
| 89 | + if (res != 0) { |
| 90 | + Logger.log ("Simulation", "-------------------------------------------------------------"); |
| 91 | + Logger.log ("Simulation", "An errors occurs in simulation mode with : see log for details [{0}]", res); |
| 92 | + throw new LauncherException ("An error occurs while running simulation"); |
| 93 | + } |
| 94 | + return ActionSequence.Continue; |
| 95 | + } |
| 96 | + |
| 97 | + public string Name { get { return configuration.Name; } } |
| 98 | + |
| 99 | + private bool configureVerboseRun() |
| 100 | + { |
| 101 | + if (Launcher.Options.verbose_run) { |
| 102 | + Env.Export (Env.GetTag("Run"), "ARCANE_TRACE_TIMER", "TRUE"); |
| 103 | + } else { |
| 104 | + Env.Export (Env.GetTag("Run"), "ARCANE_TRACE_TIMER", null); |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + public int runApplication(int? n, string arg, Env.Tag mainTag) |
| 110 | + { |
| 111 | + string cmd; |
| 112 | + List<string> cmd_args = new List<string> (); |
| 113 | + |
| 114 | + List<Env.Tag> tags = new List<Env.Tag> (); |
| 115 | + tags.Add (mainTag); |
| 116 | + tags.Add (Env.GetTag("Run")); |
| 117 | + |
| 118 | + if (n != null) { |
| 119 | + cmd = Launcher.Options.mpirun_path; |
| 120 | + cmd_args.AddRange(Launcher.Options.mpirun_extra_options); |
| 121 | + cmd_args.AddRange(ExecTools.toArgs( "-np", n )); |
| 122 | + cmd_args.AddRange(ExecTools.toArgs( "-genv", "ARCANE_PARALLEL_SERVICE", "Mpi" )); |
| 123 | + if (ExecTools.isWindows()) cmd_args.Add("-genvall"); |
| 124 | + cmd_args.Add(APPLICATION_BINARY); |
| 125 | + cmd_args.Add (arg); |
| 126 | + tags.Add (Env.GetTag("Mpi")); |
| 127 | + } else { |
| 128 | + cmd = APPLICATION_BINARY; |
| 129 | + cmd_args.Add (arg); |
| 130 | + tags.Add (Env.GetTag("Mpi")); |
| 131 | + } |
| 132 | + |
| 133 | + // Cleanup fatal file before running |
| 134 | + foreach (FileInfo f in GetFatalFiles()) { |
| 135 | + f.Delete (); |
| 136 | + } |
| 137 | + |
| 138 | + int res = ExecTools.ExecProcess (cmd, cmd_args.ToArray(), tags.ToArray()); |
| 139 | + /* |
| 140 | + if (res == 0 && GetFatalFiles().Length > 0) { |
| 141 | + // TODO: voir problème de fatal_6 en exécution parallèle |
| 142 | + // fatal_6 en cas de STOP_FAILED (ex: erreur de validateurs même non fatal; voir src/ArcTem/ArcTemModule.cc) |
| 143 | + logger.log(Logger.Channel.eError, "Fatal file detected : throws an error"); |
| 144 | + return -1; |
| 145 | + } else { |
| 146 | + return res; |
| 147 | + } |
| 148 | + */ |
| 149 | + return res; |
| 150 | + } |
| 151 | + |
| 152 | + FileInfo[] GetFatalFiles() { |
| 153 | + FileInfo[] fatal_files = new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles("fatal_*"); |
| 154 | + return fatal_files; |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments