Skip to content

Commit 9277e63

Browse files
Dotnet 6 version of ApplicationLauncher for ArcGeoSim
1 parent c320416 commit 9277e63

15 files changed

+2953
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*~
2+
obj
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
// Runtime Version:4.0.30319.42000
5+
//
6+
// Changes to this file may cause incorrect behavior and will be lost if
7+
// the code is regenerated.
8+
// </auto-generated>
9+
//------------------------------------------------------------------------------
10+
11+
//
12+
//This source code was auto-generated by MonoXSD
13+
//
14+
namespace ApplicationConfiguration {
15+
16+
17+
/// <remarks/>
18+
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "0.0.0.0")]
19+
[System.SerializableAttribute()]
20+
[System.Diagnostics.DebuggerStepThroughAttribute()]
21+
[System.ComponentModel.DesignerCategoryAttribute("code")]
22+
[System.Xml.Serialization.XmlRootAttribute("application", Namespace="", IsNullable=false)]
23+
public partial class ApplicationConfiguration {
24+
25+
private string nameField;
26+
27+
private ConfigLocation configLocationField;
28+
29+
private bool enableParallelModeField;
30+
31+
/// <remarks/>
32+
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
33+
public string Name {
34+
get {
35+
return this.nameField;
36+
}
37+
set {
38+
this.nameField = value;
39+
}
40+
}
41+
42+
/// <remarks/>
43+
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
44+
public ConfigLocation ConfigLocation {
45+
get {
46+
return this.configLocationField;
47+
}
48+
set {
49+
this.configLocationField = value;
50+
}
51+
}
52+
53+
/// <remarks/>
54+
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
55+
public bool EnableParallelMode {
56+
get {
57+
return this.enableParallelModeField;
58+
}
59+
set {
60+
this.enableParallelModeField = value;
61+
}
62+
}
63+
}
64+
65+
/// <remarks/>
66+
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "0.0.0.0")]
67+
[System.SerializableAttribute()]
68+
public enum ConfigLocation {
69+
70+
/// <remarks/>
71+
ApplicationSide,
72+
73+
/// <remarks/>
74+
Local,
75+
}
76+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3+
xmlns:ags="http://www.ifpen.fr/ArcGeoSim/Common.xsd"
4+
elementFormDefault="unqualified">
5+
6+
<!-- Fichier à transformer en objet C# via la commande
7+
/soft/irsrvsoft1/expl/Mono/Mono_3.2.8/bin/xsd ApplicationConfiguration.xsd /c /l:CS /n:Application
8+
-->
9+
10+
<xs:simpleType name="ConfigLocation" final="restriction" >
11+
<xs:restriction base="xs:string">
12+
<xs:enumeration value="ApplicationSide" />
13+
<xs:enumeration value="Local" />
14+
</xs:restriction>
15+
</xs:simpleType>
16+
17+
<xs:complexType name="ApplicationConfiguration">
18+
<xs:sequence>
19+
<xs:element name="Name" type="xs:string" />
20+
<xs:element name="ConfigLocation" type="ConfigLocation" />
21+
<xs:element name="EnableParallelMode" type="xs:boolean" />
22+
</xs:sequence>
23+
</xs:complexType>
24+
25+
<xs:element name="application" type="ApplicationConfiguration" />
26+
27+
</xs:schema>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>disable</Nullable>
8+
<Deterministic>True</Deterministic>
9+
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
10+
<SelfContained>true</SelfContained>
11+
<PublishSingleFile>true</PublishSingleFile>
12+
</PropertyGroup>
13+
14+
</Project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.IO;
3+
using System.Collections.Generic;
4+
using System.Reflection;
5+
6+
namespace ApplicationLauncher
7+
{
8+
static class AssemblyResolver
9+
{
10+
public static void Initialize() {
11+
Logger.log (Logger.Channel.DevDebug, "Initializing AssemblyResolver");
12+
Assembly thisAssembly = Assembly.GetExecutingAssembly();
13+
String thisPath = thisAssembly.Location;
14+
String directory = Path.GetDirectoryName(thisPath);
15+
16+
AddDirectory (directory);
17+
18+
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler( ResolveEventHandler );
19+
}
20+
21+
static public void AddDirectory(string directory) {
22+
if(!m_directories.Contains(directory))
23+
m_directories.Add(directory);
24+
}
25+
26+
private static Assembly ResolveEventHandler( Object sender, ResolveEventArgs args ){
27+
Assembly newAssembly = null;
28+
String newAssemblyName = null;
29+
30+
String[] onlyName = args.Name.Split(',');
31+
if (onlyName.Length == 0)
32+
return newAssembly;
33+
else
34+
newAssemblyName = onlyName [0];
35+
36+
try {
37+
Assembly thisAssembly = Assembly.GetExecutingAssembly();
38+
String thisPath = thisAssembly.Location;
39+
40+
foreach(String directory in m_directories)
41+
{
42+
String pathToManagedAssembly = Path.Combine(directory, onlyName[0] + ".dll");
43+
if(File.Exists(pathToManagedAssembly)){
44+
newAssembly = Assembly.LoadFile(pathToManagedAssembly);
45+
break; // or continue if try a another path in m_directories list
46+
}
47+
}
48+
49+
if (newAssembly != null)
50+
Logger.log (Logger.Channel.DevDebug, "Resolving assembly '{0}' with '{1}'", newAssemblyName, newAssembly.Location);
51+
else
52+
Logger.log (Logger.Channel.DevDebug, "Resolving assembly '{0}' failed", newAssemblyName);
53+
} catch (Exception e) {
54+
Logger.log(Logger.Channel.DevDebug, "Error while resolving assembly '{0}' : {1}", newAssemblyName, e.Message);
55+
}
56+
return newAssembly;
57+
}
58+
59+
private static List<String> m_directories = new List<String>();
60+
}
61+
}
62+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Text;
3+
using System.Text.RegularExpressions;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Xml;
7+
using System.Xml.Schema;
8+
using System.Xml.Serialization;
9+
using System.Reflection;
10+
11+
namespace Application
12+
{
13+
public class ConfigurationReader
14+
{
15+
public static T Initialize<T> (Stream file)
16+
where T : class, new()
17+
{
18+
string xsd_resource_name = "ApplicationLauncher.ApplicationConfiguration.xsd"; // with namespace
19+
Assembly assembly = Assembly.GetExecutingAssembly ();
20+
21+
XmlReaderSettings settings = new XmlReaderSettings ();
22+
using (Stream stream = assembly.GetManifestResourceStream(xsd_resource_name)) {
23+
if (stream == null)
24+
throw new ApplicationLauncher.InternalException (String.Format ("Cannot open resource '{0}'", xsd_resource_name));
25+
XmlSchema schema = XmlSchema.Read (stream, null);
26+
settings.Schemas.Add (schema);
27+
}
28+
settings.ValidationType = ValidationType.Schema;
29+
settings.ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.ReportValidationWarnings;
30+
31+
XmlSerializer serializer = new XmlSerializer (typeof(T));
32+
using (XmlReader reader = XmlReader.Create(file, settings)) {
33+
return serializer.Deserialize (reader) as T;
34+
}
35+
}
36+
}
37+
}
38+

0 commit comments

Comments
 (0)