public void UpdateGPUStats() { int NDevices = GPU.GetDeviceCount(); string[] Stats = new string[NDevices]; for (int i = 0; i < NDevices; i++) { Stats[i] = "GPU" + i + ": " + GPU.GetFreeMemory(i) + " MB"; } GPUStats = string.Join(", ", Stats); }
void PutFineOnDevice() { if (FineNodes == null) { throw new Exception("No fine data available."); } d_FineNodes = GPU.MallocDeviceFromHost(Helper.ToInterleaved(FineNodes), NFineNodes * 3); d_FineIntensities = GPU.MallocDeviceFromHost(FineIntensities, NFineNodes); d_FineNodeNeighbors = GPU.MallocDeviceFromHostInt(FineNodeNeighbors, NFineNodes * NFineNeighbors); d_FineNodeNeighborWeights = GPU.MallocDeviceFromHost(FineNodeNeighborWeights, NFineNodes * NFineNeighbors); }
public Image AsScaled(int3 newDims) { Image Output = new Image(IntPtr.Zero, newDims); GPU.Scale(GetDevice(Intent.Read), Output.GetDevice(Intent.Write), new int3(Dims), new int3(newDims), 1); return(Output); }
public Image AsScaled(int2 newSliceDims) { int3 Scaled = new int3(newSliceDims.X, newSliceDims.Y, Dims.Z); Image Output = new Image(IntPtr.Zero, Scaled); GPU.Scale(GetDevice(Intent.Read), Output.GetDevice(Intent.Write), new int3(DimsSlice), new int3(newSliceDims), (uint)Dims.Z); return(Output); }
public Image AsSum1D() { if (IsComplex || IsHalf) { throw new Exception("Data type not supported."); } Image Result = new Image(IntPtr.Zero, new int3(Dims.Y * Dims.Z, 1, 1)); GPU.Sum(GetDevice(Intent.Read), Result.GetDevice(Intent.Write), (uint)ElementsLineReal, (uint)(Dims.Y * Dims.Z)); return(Result); }
public Image AsIFFT(bool isvolume = false) { if (IsHalf || !IsComplex || !IsFT) { throw new Exception("Data format not supported."); } Image IFFT = new Image(IntPtr.Zero, Dims, false, false, false); GPU.IFFT(GetDevice(Intent.Read), IFFT.GetDevice(Intent.Write), isvolume ? Dims : Dims.Slice(), isvolume ? 1 : (uint)Dims.Z); return(IFFT); }
public void Project(int2 dims, float3[] angles, Image result) { PutTexturesOnDevice(); result.Fill(0); GPU.ProjectForwardTex(t_DataRe, t_DataIm, result.GetDevice(Intent.Write), Data.Dims, dims, Helper.ToInterleaved(angles), Oversampling, (uint)angles.Length); }
public Image AsAmplitudes() { if (IsHalf || !IsComplex) { throw new Exception("Data type not supported."); } Image Amplitudes = new Image(IntPtr.Zero, Dims, IsFT, false, false); GPU.Amplitudes(GetDevice(Intent.Read), Amplitudes.GetDevice(Intent.Write), ElementsComplex); return(Amplitudes); }
private void Multiply(Image multiplicators, uint elements, uint batch) { if (ElementsComplex != elements * batch || multiplicators.ElementsComplex != elements || //IsFT != multiplicators.IsFT || multiplicators.IsComplex) { throw new DimensionMismatchException(); } if (!IsComplex) { if (IsHalf && multiplicators.IsHalf) { GPU.MultiplySlicesHalf(GetDevice(Intent.Read), multiplicators.GetDevice(Intent.Read), GetDevice(Intent.Write), elements, batch); } else if (!IsHalf && !multiplicators.IsHalf) { GPU.MultiplySlices(GetDevice(Intent.Read), multiplicators.GetDevice(Intent.Read), GetDevice(Intent.Write), elements, batch); } else { Image ThisSingle = AsSingle(); Image MultiplicatorsSingle = multiplicators.AsSingle(); GPU.MultiplySlices(ThisSingle.GetDevice(Intent.Read), MultiplicatorsSingle.GetDevice(Intent.Read), ThisSingle.GetDevice(Intent.Write), elements, batch); if (IsHalf) { GPU.HalfToSingle(ThisSingle.GetDevice(Intent.Read), GetDevice(Intent.Write), elements * batch); } else { GPU.CopyDeviceToDevice(ThisSingle.GetDevice(Intent.Read), GetDevice(Intent.Write), elements * batch); } ThisSingle.Dispose(); MultiplicatorsSingle.Dispose(); } } else { if (IsHalf) { throw new Exception("Complex multiplication not supported for fp16."); } GPU.MultiplyComplexSlicesByScalar(GetDevice(Intent.Read), multiplicators.GetDevice(Intent.Read), GetDevice(Intent.Write), elements, batch); } }
public Image AsReducedAlongY() { Image Reduced = new Image(IntPtr.Zero, new int3(Dims.X, 1, Dims.Z), IsFT, IsComplex, IsHalf); if (IsHalf) { GPU.ReduceMeanHalf(GetDevice(Intent.Read), Reduced.GetDevice(Intent.Write), (uint)(DimsEffective.X * (IsComplex ? 2 : 1)), (uint)Dims.Y, (uint)Dims.Z); } else { GPU.ReduceMean(GetDevice(Intent.Read), Reduced.GetDevice(Intent.Write), (uint)(DimsEffective.X * (IsComplex ? 2 : 1)), (uint)Dims.Y, (uint)Dims.Z); } return(Reduced); }
public Image AsReducedAlongZ() { Image Reduced = new Image(IntPtr.Zero, new int3(Dims.X, Dims.Y, 1), IsFT, IsComplex, IsHalf); if (IsHalf) { GPU.ReduceMeanHalf(GetDevice(Intent.Read), Reduced.GetDevice(Intent.Write), (uint)ElementsSliceReal, (uint)Dims.Z, 1); } else { GPU.ReduceMean(GetDevice(Intent.Read), Reduced.GetDevice(Intent.Write), (uint)ElementsSliceReal, (uint)Dims.Z, 1); } return(Reduced); }
public void Reconstruct(IntPtr d_reconstruction, bool isctf, string symmetry = "C1", int planForw = -1, int planBack = -1, int planForwCTF = -1, int griddingiterations = 10) { GPU.BackprojectorReconstructGPU(Dims, DimsOversampled, Oversampling, Data.GetDevice(Intent.Read), Weights.GetDevice(Intent.Read), symmetry, isctf, d_reconstruction, planForw, planBack, planForwCTF, griddingiterations); }
public void SetVolumeFrom(Image data) { if (Volume == null || Volume.Dims != data.Dims) { Volume?.Dispose(); FreeOnDevice(); Volume = data.GetCopyGPU(); } else { GPU.CopyDeviceToDevice(data.GetDevice(Intent.Read), Volume.GetDevice(Intent.Write), data.ElementsReal); } }
public void LogEnvironment() { if (!AllowCollection) { return; } Task.Run(() => { try { int CPUCores = Environment.ProcessorCount; ulong CPUMemory = 0; ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Capacity FROM Win32_PhysicalMemory"); foreach (ManagementObject WniPART in searcher.Get()) { CPUMemory += Convert.ToUInt64(WniPART.Properties["Capacity"].Value); } CPUMemory >>= 20; int CPUClock = 0; using (ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'")) CPUClock = (int)(uint)(Mo["MaxClockSpeed"]); int GPUCores = GPU.GetDeviceCount(); int GPUMemory = (int)GPU.GetTotalMemory(0); IntPtr NamePtr = GPU.GetDeviceName(0); string GPUName = new string(Marshal.PtrToStringAnsi(NamePtr).Take(48).ToArray()); CPU.HostFree(NamePtr); Version Version = Assembly.GetExecutingAssembly().GetName().Version; new WarpAnalyticsClient().LogEnvironment(Secret, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), Version.ToString(), CPUCores, CPUClock, (int)CPUMemory, GPUCores, GPUMemory, GPUName); } catch { } }); }
public void Dispose() { lock (Sync) { if (_DeviceData != IntPtr.Zero) { GPU.FreeDevice(_DeviceData); GPU.OnMemoryChanged(); _DeviceData = IntPtr.Zero; IsDeviceDirty = false; } _HostData = null; IsHostDirty = false; } }
private void DisposeTextures() { if (t_DataRe > 0) { GPU.DestroyTexture(t_DataRe, a_DataRe); t_DataRe = 0; a_DataRe = 0; } if (t_DataIm > 0) { GPU.DestroyTexture(t_DataIm, a_DataIm); t_DataIm = 0; a_DataIm = 0; } }
public void Xray(float ndevs) { if (IsComplex || IsHalf) { throw new Exception("Complex and half are not supported."); } for (int i = 0; i < Dims.Z; i++) { GPU.Xray(new IntPtr((long)GetDevice(Intent.Read) + DimsEffective.ElementsSlice() * i * sizeof(float)), new IntPtr((long)GetDevice(Intent.Write) + DimsEffective.ElementsSlice() * i * sizeof(float)), ndevs, new int2(DimsEffective), 1); } }
public Image Project(int3 dims, float3[] angles) { PutTexturesOnDevice(); Image Result = new Image(IntPtr.Zero, new int3(dims.X, dims.Y, dims.Z * angles.Length), true, true); GPU.ProjectForward3DTex(t_DataRe, t_DataIm, Result.GetDevice(Intent.Write), Data.Dims, dims, Helper.ToInterleaved(angles), Oversampling, (uint)angles.Length); return(Result); }
public Image AsAnisotropyCorrected(int2 dimsscaled, float majorpixel, float minorpixel, float majorangle, uint supersample) { Image Corrected = new Image(IntPtr.Zero, new int3(dimsscaled.X, dimsscaled.Y, Dims.Z)); GPU.CorrectMagAnisotropy(GetDevice(Intent.Read), DimsSlice, Corrected.GetDevice(Intent.Write), dimsscaled, majorpixel, minorpixel, majorangle, supersample, (uint)Dims.Z); return(Corrected); }
public Image AsHalf() { Image Result; if (!IsHalf) { Result = new Image(IntPtr.Zero, Dims, IsFT, IsComplex, true); GPU.SingleToHalf(GetDevice(Intent.Read), Result.GetDevice(Intent.Write), ElementsReal); } else { Result = new Image(GetDevice(Intent.Read), Dims, IsFT, IsComplex, true); } return(Result); }
public Image AsScaledMassive(int2 newSliceDims) { int3 Scaled = new int3(newSliceDims.X, newSliceDims.Y, Dims.Z); Image Output = new Image(Scaled); for (int z = 0; z < Dims.Z; z++) { GPU.Scale(GetDeviceSlice(z, Intent.Read), Output.GetDeviceSlice(z, Intent.Write), Dims.Slice(), new int3(newSliceDims), 1); } return(Output); }
public Image Reconstruct(bool isctf, int planForw = -1, int planBack = -1, int planForwCTF = -1) { Image Reconstruction = new Image(IntPtr.Zero, Dims, isctf); GPU.BackprojectorReconstructGPU(Dims, DimsOversampled, Oversampling, Data.GetDevice(Intent.Read), Weights.GetDevice(Intent.Read), isctf, Reconstruction.GetDevice(Intent.Write), planForw, planBack, planForwCTF); return(Reconstruction); }
public void Train(IntPtr d_source, IntPtr d_target, IntPtr d_weightSource, IntPtr d_weightTarget, float dropoutRate, float learningRate, float orthogonalityRate, int threadID, out float[] prediction, out float[] bottleneck, out float[] loss, out float[] lossKL) { if (!ForTraining) { throw new Exception("Network was loaded in prediction mode, but asked to train."); } GPU.CopyDeviceToHostPinned(d_source, TensorSource[threadID].Data, BatchSize * (int)BoxDimensions.ElementsFFT() * 2); GPU.CopyDeviceToHostPinned(d_target, TensorTarget[threadID].Data, BatchSize * (int)BoxDimensions.ElementsFFT() * 2); GPU.CopyDeviceToHostPinned(d_weightSource, TensorWeightSource[threadID].Data, BatchSize * (int)BoxDimensions.ElementsFFT()); GPU.CopyDeviceToHostPinned(d_weightTarget, TensorWeightTarget[threadID].Data, BatchSize * (int)BoxDimensions.ElementsFFT()); Marshal.Copy(new[] { dropoutRate }, 0, TensorDropoutRate[threadID].Data, 1); Marshal.Copy(new[] { learningRate }, 0, TensorLearningRate[threadID].Data, 1); Marshal.Copy(new[] { orthogonalityRate }, 0, TensorOrthogonalityRate[threadID].Data, 1); var Output = RunnerTraining[threadID].Run(); Marshal.Copy(Output[0].Data, ResultPredicted[threadID], 0, BatchSize * (int)BoxDimensions.ElementsFFT() * 2); Marshal.Copy(Output[1].Data, ResultLoss[threadID], 0, 1); Marshal.Copy(Output[2].Data, ResultLossKL[threadID], 0, 1); Marshal.Copy(Output[3].Data, ResultBottleneck[threadID], 0, BatchSize * BottleneckWidth); prediction = ResultPredicted[threadID]; bottleneck = ResultBottleneck[threadID]; loss = ResultLoss[threadID]; lossKL = ResultLossKL[threadID]; foreach (var tensor in Output) { tensor.Dispose(); } }
private void Divide(Image divisors, uint elements, uint batch) { if (ElementsComplex != elements * batch || divisors.ElementsComplex != elements || //IsFT != divisors.IsFT || divisors.IsComplex) { throw new DimensionMismatchException(); } if (!IsComplex) { if (!IsHalf && !divisors.IsHalf) { GPU.DivideSlices(GetDevice(Intent.Read), divisors.GetDevice(Intent.Read), GetDevice(Intent.Write), elements, batch); } else { Image ThisSingle = AsSingle(); Image DivisorsSingle = divisors.AsSingle(); GPU.DivideSlices(ThisSingle.GetDevice(Intent.Read), DivisorsSingle.GetDevice(Intent.Read), ThisSingle.GetDevice(Intent.Write), elements, batch); if (IsHalf) { GPU.HalfToSingle(ThisSingle.GetDevice(Intent.Read), GetDevice(Intent.Write), elements * batch); } else { GPU.CopyDeviceToDevice(ThisSingle.GetDevice(Intent.Read), GetDevice(Intent.Write), elements * batch); } ThisSingle.Dispose(); DivisorsSingle.Dispose(); } } else { if (IsHalf) { throw new Exception("Complex division not supported for fp16."); } GPU.DivideComplexSlicesByScalar(GetDevice(Intent.Read), divisors.GetDevice(Intent.Read), GetDevice(Intent.Write), elements, batch); } }
public float3[][] GetLastSimulationResults() { if (d_LastSimulationResult == IntPtr.Zero) { return(null); } float[] RawData = new float[NCoarseNodes * 3 * NSimulatedInstances]; GPU.CopyDeviceToHost(d_LastSimulationResult, RawData, RawData.Length); float3[][] Result = new float3[NSimulatedInstances][]; for (int n = 0; n < NSimulatedInstances; n++) { Result[n] = Helper.FromInterleaved3(RawData.Skip(n * NCoarseNodes * 3).Take(NCoarseNodes * 3).ToArray()); } return(Result); }
public void FreeOnDevice() { if (t_Volume > 0) { GPU.DestroyTexture(t_Volume, a_Volume); t_Volume = 0; a_Volume = 0; } if (t_Coloring > 0) { GPU.DestroyTexture(t_Coloring, a_Coloring); t_Coloring = 0; a_Coloring = 0; } ColorMapImage?.FreeDevice(); }
public Image AsProjections(float3[] angles, int2 dimsprojection, float supersample) { if (Dims.X != Dims.Y || Dims.Y != Dims.Z) { throw new Exception("Volume must be a cube."); } Image Projections = new Image(IntPtr.Zero, new int3(dimsprojection.X, dimsprojection.Y, angles.Length), true, true); GPU.ProjectForward(GetDevice(Intent.Read), Projections.GetDevice(Intent.Write), Dims, dimsprojection, Helper.ToInterleaved(angles), supersample, (uint)angles.Length); return(Projections); }
public Image Reconstruct(bool isctf, string symmetry = "C1", int planForw = -1, int planBack = -1, int planForwCTF = -1, int griddingiterations = 10) { Image Reconstruction = new Image(IntPtr.Zero, Dims, isctf); GPU.BackprojectorReconstructGPU(Dims, DimsOversampled, Oversampling, Data.GetDevice(Intent.Read), Weights.GetDevice(Intent.Read), symmetry, isctf, Reconstruction.GetDevice(Intent.Write), planForw, planBack, planForwCTF, griddingiterations); return(Reconstruction); }
public void BackProject(Image projft, Image projweights, float3[] angles) { if (!projft.IsFT || !projft.IsComplex || !projweights.IsFT) { throw new Exception("Input data must be complex (except weights) and in FFTW layout."); } float[] Angles = Helper.ToInterleaved(angles); GPU.ProjectBackward(Data.GetDevice(Intent.ReadWrite), Weights.GetDevice(Intent.ReadWrite), DimsOversampled, projft.GetDevice(Intent.Read), projweights.GetDevice(Intent.Read), projft.DimsSlice, projft.Dims.X / 2, Angles, Oversampling, (uint)projft.Dims.Z); }
public void Predict(IntPtr d_data, int threadID, out long[] argmax, out float[] probability) { //if (ForTraining) // throw new Exception("Network was loaded in training mode, but asked to predict."); GPU.CopyDeviceToHostPinned(d_data, TensorMicTilePredict[threadID].Data, BatchSize * (int)BoxDimensionsPredict.Elements()); var Output = RunnerPrediction[threadID].Run(); Marshal.Copy(Output[0].Data, ResultArgMax[threadID], 0, BatchSize * (int)BoxDimensionsPredict.Elements()); Marshal.Copy(Output[1].Data, ResultSoftMax[threadID], 0, BatchSize * (int)BoxDimensionsPredict.Elements() * 3); argmax = ResultArgMax[threadID]; probability = ResultSoftMax[threadID]; foreach (var tensor in Output) { tensor.Dispose(); } }