protected static void ThreadedRepeat(int count, Action <int> action) { Exception exception = null; Forker p = new Forker(); p.ItemComplete += delegate(object sender, ParallelEventArgs args) { if (args.Exception != null && exception == null) { exception = args.Exception; } }; for (int i = 0; i < count; i++) { Action <int> tmp = action; int index = i; p.Fork(() => tmp(index)); } p.Join(); if (exception != null) { throw exception; } }
//void CalculateAtmosphereTimeStep(bool debug = false) //{ // DateTime time = DateTime.Now; // //We simply calculate each one from the values around them, so they will have old and new value used // for (int x = 0; x < WORLD_SIZE; x++) // { // for (int y = 0; y < WORLD_SIZE; y++) // { // for (int z = 0; z < 20 / DZ; z++) // { // //See https://en.wikipedia.org/wiki/Primitive_equations // int position = z + 20 * x + y * WORLD_SIZE * 20; // WeatherPoint point = atmosphere[position]; // // du/dt -fv = -d geopotential / dx // WeatherPoint right = atmosphere[z + 20 * (x + 1) + y + WORLD_SIZE * 20]; // WeatherPoint left = atmosphere[z + 20 * (x - 1) + y + WORLD_SIZE * 20]; // float dU = atmosphereTimeStep * point.f * (point.v + 0.001) - (right.geopotential - left.geopotential) / (2 * DX); // point.u += dU; // // dv/dt + fu = -d geopotential / dy // WeatherPoint bottom = atmosphere[z + 20 * x + (y + 1) + WORLD_SIZE * 20]; // WeatherPoint top = atmosphere[z + 20 * x + (y - 1) + WORLD_SIZE * 20]; // float dV = atmosphereTimeStep * point.f * point.u - (bottom.geopotential - top.geopotential) / (2 * DX); // point.v += dV; // // p = air densiy * R * T // float pressure = point.pressure; // point.pressure = 1013 * Math.Exp(-0.02896 * 9.807 / (R * 288.15) * (273 + point.temperature)); //See https://www.math24.net/barometric-formula/; // float dPressures = pressure - point.pressure; // // d geopotential = d pressure * ( R * T / p) // float dGeoPotential = dPressures * (R * point.temperature / point.pressure); // point.geopotential += dGeoPotential; // // d vertical = d pressure * ( -dU / dx -dV / dy) // float dW = dPressures * (-dU / DX - dV / DY); // point.w += dW; // // dT (1 /dt + u/dx + v/dy + w/dp - w*R*T/(p*cp)) = J/cp // // dT = J / (cp * (1 / dt + u/dx + v/dy +w /dp - w*R*T/(p * pc)) // // I guess J is simply energy balance // float dT = point.EnergyBalance / (10 * WeatherPoint.specificHeat * // (1d / atmosphereTimeStep + point.u / dU + point.v / dV + point.w / dPressures - point.w * R * point.temperature / (point.pressure * WeatherPoint.specificHeat))); // if (!float.IsNaN(dT)) // { // if (Math.Abs(dT) > 4) // point.temperature += Math.Sign(dT) * 4; // //throw new Exception(); // point.temperature += dT; // } // else // point.temperature += 0.0001; // if (point.temperature == 0 && point.pressure == 0 && point.u == 0 && point.v == 0 && // point.geopotential == 0 && point.w == 0) // throw new Exception(); // if (float.IsNaN(point.temperature)) // throw new Exception(); // if (float.IsNaN(point.pressure)) // throw new Exception(); // if (float.IsNaN(point.u)) // throw new Exception(); // if (float.IsNaN(point.v)) // throw new Exception(); // if (float.IsNaN(point.geopotential)) // throw new Exception(); // if (float.IsNaN(point.w)) // throw new Exception(); // atmosphere[position] = point; // } // } // } // if (debug) // Trace.TraceInformation($"Atmosphere calculation in {(DateTime.Now - time).TotalMilliseconds}"); //} #endregion OldCode private void CalculateAtmosphereTimeStep(bool debug = false) { DateTime time = DateTime.Now; //CalculateWeather(0, WORLD_SIZE); Forker forker = new Forker(); //totalHumidity = 0; //TODO: Move the boundaries between threads, so that artifacts are cleaned up for (int i = 0; i < THREADS; i++) { int startY = i * WORLD_SIZE / THREADS; int endY = (i + 1) * WORLD_SIZE / THREADS; forker.Fork(() => CalculateWeather(startY, endY)); } forker.Join(); //Trace.TraceInformation("Total humidity: " + totalHumidity); if (debug) { Trace.TraceInformation($"Atmosphere calculation in {(DateTime.Now - time).TotalMilliseconds}"); } }
public static void Init() { plugins.Clear(); List <PluginMetadata> pluginMetadatas = PluginConfigLoader.ParsePluginsConfig(); plugins.AddRange(new CSharpPluginLoader().LoadPlugin(pluginMetadatas)); plugins.AddRange(new BasePluginLoader <PythonPlugin>().LoadPlugin(pluginMetadatas)); Forker forker = new Forker(); foreach (PluginPair pluginPair in plugins) { PluginPair pair = pluginPair; forker.Fork(() => pair.Plugin.Init(new PluginInitContext() { CurrentPluginMetadata = pair.Metadata, Proxy = HttpProxy.Instance, API = App.Window })); } forker.Join(); }
protected static void ThreadedRepeat(int count, Action <int, IThreadAsserter> action) { Forker p = new Forker(); Exception exception = null; List <IThreadAsserter> asserts = new List <IThreadAsserter>(count); p.ItemComplete += delegate(object sender, ParallelEventArgs args) { if (args.Exception != null && exception == null) { exception = args.Exception; } }; for (int i = 0; i < count; i++) { Action <int, IThreadAsserter> tmp = action; ThreadAsserter asserter = new ThreadAsserter(); asserts.Add(asserter); int index = i; p.Fork(() => tmp(index, asserter)); } p.Join(); if (exception != null) { throw exception; } foreach (IThreadAsserter threadAsserter in asserts) { threadAsserter.Run(); threadAsserter.Dispose(); } }
/// <summary> /// This is the main workhorse method which runs in another thread. /// </summary> /// <remarks> /// Takes the MeshInput(s) and converts them to TransitionMeshes in parallel. It then creates optimized /// MeshOutput(s) for use later or through the callback. /// </remarks> protected sealed override void ThreadedFunction() { // Empty out preexisting parsed data _transitionMeshes.Clear(); _meshOutputs.Clear(); // Clever forker solution since we don't have Parallel.ForEach support available. var parallelTasks = new Forker(); foreach (var meshInput in _meshInputs) { var tempInput = meshInput; parallelTasks.Fork(delegate { CreateTransitionMesh(tempInput); }); } parallelTasks.Join(); // Sort the meshes in order _transitionMeshes.Sort(new TransitionMeshSorter()); var meshOutput = new MeshOutput(); int bitmask = _transitionMeshes [0].GetBitMask(); foreach (var transitionMesh in _transitionMeshes) { if (transitionMesh.GetBitMask() != bitmask || (transitionMesh.VertexCount + meshOutput.VertexCount) > Mesh.VerticesArrayLimit) { _meshOutputs.Add(meshOutput); meshOutput = new MeshOutput(); } var baseIndex = meshOutput.VertexCount; meshOutput.VertexCount += transitionMesh.VertexCount; meshOutput.SortedSources.Add(transitionMesh); meshOutput.Vertices.AddRange(transitionMesh.Vertices); if (transitionMesh.Normals != null) { meshOutput.Normals.AddRange(transitionMesh.Normals); } if (transitionMesh.Colors != null) { meshOutput.Colors.AddRange(transitionMesh.Colors); } if (transitionMesh.Tangents != null) { meshOutput.Tangents.AddRange(transitionMesh.Tangents); } if (transitionMesh.UV != null) { meshOutput.UV.AddRange(transitionMesh.UV); } if (transitionMesh.UV1 != null) { meshOutput.UV1.AddRange(transitionMesh.UV1); } if (transitionMesh.UV2 != null) { meshOutput.UV2.AddRange(transitionMesh.UV2); } var indexes = meshOutput.GetSubMesh(transitionMesh.Material); indexes.Capacity = indexes.Count + transitionMesh.IndexCount; for (var i = 0; i < transitionMesh.IndexCount; i++) { indexes.Add(baseIndex + transitionMesh.Indexes [i]); } bitmask = transitionMesh.GetBitMask(); } _meshOutputs.Add(meshOutput); }
public static void Init() { if (initializing != null) { return; } initializing = new ManualResetEvent(false); plugins.Clear(); BasePluginLoader.ParsePluginsConfig(); if (UserSettingStorage.Instance.EnablePythonPlugins) { plugins.AddRange(new PythonPluginLoader().LoadPlugin()); } plugins.AddRange(new CSharpPluginLoader().LoadPlugin()); Forker forker = new Forker(); foreach (IPlugin plugin in plugins.Select(pluginPair => pluginPair.Plugin)) { IPlugin plugin1 = plugin; PluginPair pluginPair = plugins.FirstOrDefault(o => o.Plugin == plugin1); if (pluginPair != null) { PluginMetadata metadata = pluginPair.Metadata; pluginPair.InitContext = new PluginInitContext() { Plugins = plugins, CurrentPluginMetadata = metadata, ChangeQuery = s => App.Window.Dispatcher.Invoke(new Action(() => App.Window.ChangeQuery(s))), CloseApp = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.CloseApp())), HideApp = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.HideApp())), ShowApp = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.ShowApp())), ShowMsg = (title, subTitle, iconPath) => App.Window.Dispatcher.Invoke(new Action(() => App.Window.ShowMsg(title, subTitle, iconPath))), OpenSettingDialog = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.OpenSettingDialog())), ShowCurrentResultItemTooltip = (msg) => App.Window.Dispatcher.Invoke(new Action(() => App.Window.ShowCurrentResultItemTooltip(msg))), ReloadPlugins = () => App.Window.Dispatcher.Invoke(new Action(() => Init())), InstallPlugin = (filePath) => App.Window.Dispatcher.Invoke(new Action(() => { PluginInstaller.Install(filePath); })), StartLoadingBar = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.StartLoadingBar())), StopLoadingBar = () => App.Window.Dispatcher.Invoke(new Action(() => App.Window.StopLoadingBar())), //ShellRun = (cmd) => (bool)App.Window.Dispatcher.Invoke(new Func<bool>(() => App.Window.ShellRun(cmd))) }; pluginPair.InitContext.ShellRun = (cmd) => { try { return((bool)App.Window.Dispatcher.Invoke(new Func <bool>(() => App.Window.ShellRun(cmd)))); } catch (Exception) { return(false); } }; forker.Fork(() => plugin1.Init(pluginPair.InitContext)); } } ThreadPool.QueueUserWorkItem(o => { forker.Join(); initializing.Set(); initializing = null; }); }