double OptimizeQ(IMatrix A, double[] B) { // Optimization Func <double, double> f = (double w) => { for (int i = 0; i < Q.Length; i++) { relaxQ[i] = Q[i] * w + prevQ[i] * (1.0 - w); } NSB.Build(A, B, relaxQ); A.Multiply(relaxQ, Aq); double value = VectorUtils.RelativeError(Aq, B); A.Clear(); Array.Fill(Aq, 0.0); Array.Fill(B, 0.0); return(value); }; double w = 1.0; double nextDiff = f(w); while (nextDiff >= Diff) { w /= 2; nextDiff = f(w); } Diff = nextDiff; return(w); }
/// <summary> /// executes all commands /// </summary> /// <returns> /// a reader to the standard output of the MATLAB process. /// </returns> public void Execute(bool PrintOutput = true) { ilPSP.MPICollectiveWatchDog.Watch(csMPI.Raw._COMM.WORLD); if (Executed == true) { throw new InvalidOperationException("Execute can be called only once."); } // run MATLAB // ========== Cmd("exit"); // be sure to exit! if (Rank == 0) { CommandFile.Flush(); CommandFile.Close(); //psi.RedirectStandardOutput = true; //psi.RedirectStandardError = true; //psi.RedirectStandardInput = true; var proc = Process.Start(psi); proc.WaitForExit(); } // return // ====== if (Rank == 0) { var p = Path.Combine(WorkingDirectory.FullName, LOGFILE); if (PrintOutput) { var stdout = new StreamReader(p); string line = stdout.ReadLine(); while (line != null) { Console.WriteLine(line); line = stdout.ReadLine(); } stdout.Dispose(); } CreatedFiles.Add(p); } foreach (string key in m_OutputObjects.Keys.ToArray()) { object outputObj = m_OutputObjects[key]; string filepath; if (Rank == 0) { filepath = Path.Combine(WorkingDirectory.FullName, key); } else { filepath = null; } if (outputObj is IMatrix) { // ++++++++++++++++++++++++++ // load pre-allocated matrix // ++++++++++++++++++++++++++ IMatrix outputMtx = (IMatrix)outputObj; if (Rank == 0) { outputMtx.LoadFromTextFile(filepath); } var _outputMtx = ilPSP.MPIEnviroment.Broadcast(outputMtx, 0, csMPI.Raw._COMM.WORLD); if (Rank != 0) { outputMtx.Clear(); outputMtx.Acc(1.0, _outputMtx); } if (!object.ReferenceEquals(outputObj, outputMtx)) { } } else if (outputObj is Type && ((Type)outputObj) == typeof(MultidimensionalArray)) { // ++++++++++++++++++++++++++ // load matrix which is NOT pre-allocated, unknown size // ++++++++++++++++++++++++++ IMatrix outputMtx = null; if (Rank == 0) { outputMtx = IMatrixExtensions.LoadFromTextFile(filepath); } var _outputMtx = ilPSP.MPIEnviroment.Broadcast(outputMtx, 0, csMPI.Raw._COMM.WORLD); m_OutputObjects[key] = _outputMtx; } else if (outputObj is int[][]) { int[][] outputStAry = (int[][])outputObj; if (Rank == 0) { LoadStaggeredArray(outputStAry, filepath); } var _outputStAry = ilPSP.MPIEnviroment.Broadcast(outputStAry, 0, csMPI.Raw._COMM.WORLD); if (Rank != 0) { for (int i = 0; i < Math.Max(_outputStAry.Length, outputStAry.Length); i++) // we use max to ensure an index-out-of-range if something is fishy { outputStAry[i] = _outputStAry[i]; } } } else { throw new NotImplementedException("output object type not implemented."); } } }
public void Clear() { matrix11.Clear(); matrix12.Clear(); matrix22.Clear(); }