/// <summary> /// Places the given labels. /// </summary> protected override void RunInternal() { Label[] lbs = labels.Where(l => l != null).ToArray(); StartListenToLocalProgress(lbs.Length); // Place outer most labels first, since their positions are more semantically important // Also place labels on short edges before labels on long edges, since short edges have less options. IEnumerable <Label> sortedLabels = lbs.OrderByDescending(l => Math.Abs(0.5 - l.PlacementOffset)) .ThenBy(l => edgePoints[((Edge)l.GeometryParent)].Count); #if NET4 && PARALLEL_SUPPORTED if (ParallelProcessingEnabled && lbs.Length > 50) { ParallelUtilities.ForEach(sortedLabels, PlaceLabel, ProgressSteps); } else #endif { foreach (Label label in sortedLabels) { PlaceLabel(label); ProgressStep(); } } }
public void EnsureGenerationStarted() { TimeRemoved = null; using (m_creationQueueSemaphore.AcquireSharedUsing()) if (m_creationQueued) { return; } using (m_creationQueueSemaphore.AcquireExclusiveUsing()) { if (m_creationQueued) { return; } m_creationQueued = true; } if (IsMarkedForRemoval) { return; } if (m_construction == null) { MyAPIGateway.Parallel.StartBackground(ParallelUtilities.WrapAction(() => { if (!Stage_Generate()) { return; } if (!Stage_Build()) { return; } MyAPIGateway.Utilities.InvokeOnGameThread(ParallelUtilities.WrapAction(Stage_SpawnGrid, Module)); }, Module)); } else if (m_grids == null) { MyAPIGateway.Parallel.StartBackground(ParallelUtilities.WrapAction(() => { if (!Stage_Build()) { return; } MyAPIGateway.Utilities.InvokeOnGameThread(ParallelUtilities.WrapAction(Stage_SpawnGrid, Module)); }, Module)); } else if (m_component == null) { MyAPIGateway.Utilities.InvokeOnGameThread(ParallelUtilities.WrapAction(Stage_SpawnGrid, Module)); } else { m_creationQueued = false; } }
public async Task ParallelEnqueueAndDequeueQueueBfs() { var ownerId = Guid.NewGuid().ToString("N"); var scheduler = new MySqlQueueBfsScheduler(_hashAlgorithm, new Opt()); await scheduler.InitializeAsync(ownerId); ParallelUtilities.For(0, 1000, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 20 }, async i => { await scheduler.EnqueueAsync(new[] { new Request("http://www.a.com") { Owner = ownerId } }); await scheduler.EnqueueAsync(new[] { new Request("http://www.a.com") { Owner = ownerId } }); await scheduler.EnqueueAsync(new[] { new Request("http://www.a.com") { Owner = ownerId } }); await scheduler.EnqueueAsync(new[] { new Request("http://www.b.com") { Owner = ownerId } }); var cnt = await scheduler.EnqueueAsync( new[] { new Request($"http://www.{i.ToString()}.com") { Owner = ownerId } }); _testOutputHelper.WriteLine($"Enqueue {i}: {cnt}"); }); _testOutputHelper.WriteLine($"End"); ParallelUtilities.For(0, 1000, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 20 }, async _ => { await scheduler.DequeueAsync(); }); Assert.Equal(1002, scheduler.GetTotalAsync().Result); await scheduler.CleanAsync(); }
public IMyFaction GetOrCreateFaction() { if (m_faction != null) { return(m_faction); } if (m_creationFailed) { return(null); } ParallelUtilities.InvokeOnGameThreadBlocking(() => { m_faction = MyAPIGateway.Session.Factions.TryGetFactionByTag(Tag); if (m_faction != null) { return; } var totalSpeciality = m_attributeWeight.Values.Sum(); var avgSpeciality = totalSpeciality / m_attributeWeight.Count; var specializationString = new StringBuilder(); specializationString.Append("We also specialize in "); var specials = m_attributeWeight.Where(x => x.Value > avgSpeciality).OrderByDescending(x => x.Value).Skip(1).ToList(); for (var i = 0; i < specials.Count; i++) { if (i > 0 && specials.Count > 2) { specializationString.Append(", "); } if (i > 0 && i == specials.Count - 1) { if (specials.Count <= 2) { specializationString.Append(" "); } specializationString.Append("and "); } specializationString.Append(specials[i].Key.Description); } specializationString.Append("."); m_faction = CreateNpcFaction(Tag, Name, "Your place for " + BestSpeciality.Description + ". " + specializationString, ""); }); return(m_faction); }
public void InitFromPrefab() { if (Initialized) { return; } var cob = GetObjectBuilder(); var chash = cob.ComputeHash(); Initialized = true; InitFromGrids(Prefab.CubeGrids[0], Prefab.CubeGrids); Logger.Info("Loaded {0} with {1} mount points, {2} reserved spaces, and {3} blocks. {4} aux grids", Name, MountPoints.Count(), ReservedSpaces.Count(), PrimaryGrid.CubeBlocks.Count, Prefab.CubeGrids.Length - 1); foreach (var type in MountPointTypes) { Logger.Info(" ...of type \"{0}\" there are {1}", type, MountPointsOfType(type).Count()); } var obs = GetObjectBuilder(); var nhash = obs.ComputeHash(); if (nhash == chash) { return; } MyAPIGateway.Parallel.StartBackground(ParallelUtilities.WrapAction(() => { try { Logger.Info("Invalid hash for cached definition of {0}; writing to local storage. {1} => {2}", Name, chash, nhash); using (var writer = MyAPIGateway.Utilities.WriteFileInLocalStorage(CacheName, typeof(PartFromPrefab))) { var xml = MyAPIGateway.Utilities.SerializeToXML(obs); writer.Write(xml); } } catch (Exception e) { Logger.Error("Write failed.\n{0}", e); } }, Manager)); }