private void ComposeTomoDial() { CanvasDial.Children.Clear(); TiltKnobs.Clear(); if (Movie == null || Movie.GetType() != typeof(TiltSeries)) { return; } TiltSeries Series = (TiltSeries)Movie; for (int i = 0; i < Series.NTilts; i++) { TiltDialItem Knob = new TiltDialItem(); Knob.TiltID = i; Knob.Angle = Series.Angles[i]; Knob.Dose = Series.Dose[i]; Knob.DoProcess = Series.UseTilt[i]; Knob.Width = 160; Knob.Height = 20; TransformGroup TGroup = new TransformGroup(); TGroup.Children.Add(new TranslateTransform(-80, -10)); TGroup.Children.Add(new RotateTransform(-Knob.Angle)); Knob.RenderTransform = TGroup; TiltKnobs.Add(Knob); CanvasDial.Children.Add(Knob); double Radius = ActualWidth / 2; Canvas.SetLeft(Knob, Math.Cos((Knob.Angle + 180) * Helper.ToRad) * (Radius + 85) + Radius); Canvas.SetTop(Knob, -Math.Sin((Knob.Angle + 180) * Helper.ToRad) * (Radius + 85) + Radius); Knob.SelectionChanged += (o, args) => TiltID = ((TiltDialItem)o).TiltID; Knob.DoProcessChanged += (o, args) => { Series.UseTilt[Knob.TiltID] = (bool)args.NewValue; Series.SaveMeta(); UpdateTiltDial(); }; Knob.MouseWheel += TiltDial_MouseWheel; } UpdateTiltDial(); }
static void Main(string[] args) { CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture; //if (!Debugger.IsAttached) // Debugger.Launch(); if (args.Length < 3) { return; } DeviceID = int.Parse(args[0]) % GPU.GetDeviceCount(); PipeName = args[1]; bool DebugMode = bool.Parse(args[2]); GPU.SetDevice(DeviceID); Console.WriteLine($"Running on GPU #{DeviceID} ({GPU.GetFreeMemory(DeviceID)} MB free) through {PipeName}\n"); Formatter = new BinaryFormatter(); Heartbeat = new Thread(new ThreadStart(() => { if (!DebugMode) { while (true) { try { NamedPipeClientStream PipeHeartbeat = new NamedPipeClientStream(".", PipeName + "_heartbeat", PipeDirection.In); PipeHeartbeat.Connect(5000); PipeHeartbeat.Dispose(); } catch { if (!Terminating) { Process.GetCurrentProcess().Kill(); } } } } })); Heartbeat.Start(); while (true) { PipeReceive = new NamedPipeClientStream(".", PipeName + "_out", PipeDirection.In); PipeReceive.Connect(); NamedSerializableObject Command = (NamedSerializableObject)Formatter.Deserialize(PipeReceive); PipeReceive.Dispose(); Console.WriteLine($"Received \"{Command.Name}\", with {Command.Content.Length} arguments, for GPU #{GPU.GetDevice()}, {GPU.GetFreeMemory(DeviceID)} MB free"); try { Stopwatch Watch = new Stopwatch(); Watch.Start(); if (Command.Name == "Exit") { Movie.WriteAverageAsync?.Wait(); SendSuccessStatus(true); Process.GetCurrentProcess().Kill(); return; } else if (Command.Name == "Ping") { Console.WriteLine("Ping!"); } else if (Command.Name == "SetHeaderlessParams") { HeaderlessDims = (int2)Command.Content[0]; HeaderlessOffset = (long)Command.Content[1]; HeaderlessType = (string)Command.Content[2]; Console.WriteLine($"Set headerless parameters to {HeaderlessDims}, {HeaderlessOffset}, {HeaderlessType}"); } else if (Command.Name == "LoadGainRef") { GainRef?.Dispose(); DefectMap?.Dispose(); string GainPath = (string)Command.Content[0]; bool FlipX = (bool)Command.Content[1]; bool FlipY = (bool)Command.Content[2]; bool Transpose = (bool)Command.Content[3]; string DefectsPath = (string)Command.Content[4]; if (!string.IsNullOrEmpty(GainPath)) { GainRef = LoadAndPrepareGainReference(GainPath, FlipX, FlipY, Transpose); } if (!string.IsNullOrEmpty(DefectsPath)) { DefectMap = LoadAndPrepareDefectMap(DefectsPath, FlipX, FlipY, Transpose); } Console.WriteLine($"Loaded gain reference and defect map: {GainRef}, {FlipX}, {FlipY}, {Transpose}, {DefectsPath}"); } else if (Command.Name == "LoadStack") { OriginalStack?.Dispose(); string Path = (string)Command.Content[0]; decimal ScaleFactor = (decimal)Command.Content[1]; int EERGroupFrames = (int)Command.Content[2]; HeaderEER.GroupNFrames = EERGroupFrames; OriginalStack = LoadAndPrepareStack(Path, ScaleFactor); OriginalStackOwner = Helper.PathToNameWithExtension(Path); Console.WriteLine($"Loaded stack: {OriginalStack}, {ScaleFactor}"); } else if (Command.Name == "MovieProcessCTF") { string Path = (string)Command.Content[0]; ProcessingOptionsMovieCTF Options = (ProcessingOptionsMovieCTF)Command.Content[1]; if (Helper.PathToNameWithExtension(Path) != OriginalStackOwner) { throw new Exception("Currently loaded stack doesn't match the movie requested for processing!"); } Movie M = new Movie(Path); M.ProcessCTF(OriginalStack, Options); M.SaveMeta(); Console.WriteLine($"Processed CTF for {Path}"); } else if (Command.Name == "MovieProcessMovement") { string Path = (string)Command.Content[0]; ProcessingOptionsMovieMovement Options = (ProcessingOptionsMovieMovement)Command.Content[1]; if (Helper.PathToNameWithExtension(Path) != OriginalStackOwner) { throw new Exception("Currently loaded stack doesn't match the movie requested for processing!"); } Movie M = new Movie(Path); M.ProcessShift(OriginalStack, Options); M.SaveMeta(); Console.WriteLine($"Processed movement for {Path}"); } else if (Command.Name == "MovieExportMovie") { string Path = (string)Command.Content[0]; ProcessingOptionsMovieExport Options = (ProcessingOptionsMovieExport)Command.Content[1]; if (Helper.PathToNameWithExtension(Path) != OriginalStackOwner) { throw new Exception("Currently loaded stack doesn't match the movie requested for processing!"); } Movie M = new Movie(Path); M.ExportMovie(OriginalStack, Options); M.SaveMeta(); Console.WriteLine($"Exported movie for {Path}"); } else if (Command.Name == "MovieExportParticles") { string Path = (string)Command.Content[0]; ProcessingOptionsParticlesExport Options = (ProcessingOptionsParticlesExport)Command.Content[1]; float2[] Coordinates = (float2[])Command.Content[2]; if (Helper.PathToNameWithExtension(Path) != OriginalStackOwner) { throw new Exception("Currently loaded stack doesn't match the movie requested for processing!"); } Movie M = new Movie(Path); M.ExportParticles(OriginalStack, Coordinates, Options); M.SaveMeta(); Console.WriteLine($"Exported {Coordinates.Length} particles for {Path}"); } else if (Command.Name == "TomoProcessCTF") { string Path = (string)Command.Content[0]; ProcessingOptionsMovieCTF Options = (ProcessingOptionsMovieCTF)Command.Content[1]; TiltSeries T = new TiltSeries(Path); T.ProcessCTFSimultaneous(Options); T.SaveMeta(); Console.WriteLine($"Processed CTF for {Path}"); } else if (Command.Name == "TomoExportParticles") { string Path = (string)Command.Content[0]; ProcessingOptionsTomoSubReconstruction Options = (ProcessingOptionsTomoSubReconstruction)Command.Content[1]; float3[] Coordinates = (float3[])Command.Content[2]; float3[] Angles = Command.Content[3] != null ? (float3[])Command.Content[3] : null; TiltSeries T = new TiltSeries(Path); T.ReconstructSubtomos(Options, Coordinates, Angles); T.SaveMeta(); Console.WriteLine($"Exported {Coordinates.Length} particles for {Path}"); } else if (Command.Name == "MPAPreparePopulation") { string Path = (string)Command.Content[0]; MPAPopulation = new Population(Path); foreach (var species in MPAPopulation.Species) { Console.Write($"Preparing {species.Name} for refinement... "); species.PrepareRefinementRequisites(true, DeviceID); Console.WriteLine("Done."); } } else if (Command.Name == "MPARefine") { string Path = (string)Command.Content[0]; string WorkingDirectory = (string)Command.Content[1]; string LogPath = (string)Command.Content[2]; ProcessingOptionsMPARefine Options = (ProcessingOptionsMPARefine)Command.Content[3]; DataSource Source = (DataSource)Command.Content[4]; Movie Item = null; if (Helper.PathToExtension(Path).ToLower() == ".tomostar") { Item = new TiltSeries(Path); } else { Item = new Movie(Path); } GPU.SetDevice(DeviceID); Item.PerformMultiParticleRefinement(WorkingDirectory, Options, MPAPopulation.Species.ToArray(), Source, GainRef, DefectMap, (message) => { Console.WriteLine(message); bool Success = false; int Tries = 0; while (!Success && Tries < 10) { try { using (TextWriter Writer = File.AppendText(LogPath)) Writer.WriteLine(message); Success = true; } catch { Thread.Sleep(100); Tries++; } } }); Item.SaveMeta(); GPU.CheckGPUExceptions(); Console.WriteLine($"Finished refining {Item.Name}"); } else if (Command.Name == "MPASaveProgress") { string Path = (string)Command.Content[0]; MPAPopulation.SaveRefinementProgress(Path); } else if (Command.Name == "TryAllocatePinnedMemory") { long[] ChunkSizes = (long[])Command.Content[0]; IntPtr[] Chunks = new IntPtr[ChunkSizes.Length]; for (int i = 0; i < ChunkSizes.Length; i++) { Chunks[i] = GPU.MallocHostPinned(ChunkSizes[i] / sizeof(float)); //Dummies.Add(Helper.ArrayOfSequence(0, (int)(ChunkSizes[i] / sizeof(float) / 2), 1)); } GPU.CheckGPUExceptions(); //for (int i = 0; i < ChunkSizes.Length; i++) // GPU.FreeHostPinned(Chunks[i]); Console.WriteLine($"Successfully allocated {ChunkSizes.Sum()} bytes of pinned memory"); } Watch.Stop(); Console.WriteLine((Watch.ElapsedMilliseconds / 1000f).ToString("F3")); Console.WriteLine(""); SendSuccessStatus(true); } catch (Exception e) { Console.WriteLine(e.ToString()); File.WriteAllText($"worker_{DeviceID}_crash.txt", e.ToString()); //Console.Read(); SendSuccessStatus(false); } } }