void __AddSol(ref double[] X) { Debug.Assert(SolHistory.Count == MxxHistory.Count); Debug.Assert(X.Length == OpMatrix._RowPartitioning.LocalLength); int L = X.Length; int KrylovDim = SolHistory.Count; double[] Mxx = new double[L]; OpMatrix.SpMV(1.0, X, 0.0, Mxx); for (int i = 0; i < KrylovDim; i++) { Debug.Assert(!object.ReferenceEquals(Mxx, MxxHistory[i])); double beta = BLAS.ddot(L, Mxx, 1, MxxHistory[i], 1).MPISum(); BLAS.daxpy(L, -beta, SolHistory[i], 1, X, 1); BLAS.daxpy(L, -beta, MxxHistory[i], 1, Mxx, 1); } double gamma = 1.0 / Mxx.L2NormPow2().MPISum().Sqrt(); //double gamma = 1.0 / BLAS.dnrm2(L, Mxx, 1).Pow2().MPISum().Sqrt(); BLAS.dscal(L, gamma, Mxx, 1); BLAS.dscal(L, gamma, X, 1); SolHistory.Add(X); MxxHistory.Add(Mxx); X = null; }
/// <summary> /// Solves the linear system (diag(1/<paramref name="dt"/>) + /// <em>M</em>) * x = /// <see cref="ImplicitTimeStepper.CurrentState"/> / /// <paramref name="dt"/> - /// <see cref="ImplicitTimeStepper.m_AffineOffset1"/> and writes the /// result to <see cref="ImplicitTimeStepper.CurrentState"/>. /// </summary> /// <param name="dt">The length of the timestep</param> protected override void PerformTimeStep(double dt) { using (var tr = new ilPSP.Tracing.FuncTrace()) { int n = Mapping.LocalLength; int np = m_diagVecOneSec.Length; double[] diag = new double[n]; for (int i = 0; i < n; i++) { diag[i] = m_diagVecOneSec[i % np]; } BLAS.dscal(n, 1.0 / dt, diag, 1); double[] rhs = (double[])m_AffineOffset1.Clone(); BLAS.dscal(n, -1.0, rhs, 1); for (int i = 0; i < n; i++) { rhs[i] += diag[i] * CurrentState[i]; } tr.Info("Calling solver"); LastSolverResult = m_Solver.Solve <double[], CoordinateVector, double[]>(1.0, diag, CurrentState, rhs); } }
/// <summary> /// Returns the source which contains Au_{n-1}+b for the Operator and the field provided in the constructor /// </summary> /// <param name="k">results of Ay+b</param> /// <param name="dt">optional scaling by time step size</param> public void ExplicitEulerSource(SubgridCoordinateMapping u, out double[] convectiveSource, double dt) { convectiveSource = new double[u.subgridCoordinates.Length]; u.Compress(); SubgridOperatorMatr.SpMVpara <double[], double[]>(dt, u.subgridCoordinates, 1.0, convectiveSource); BLAS.daxpy(SubgridAffine.Length, dt, SubgridAffine, 1, convectiveSource, 1); }
public void SwapTest(float?value) { Vector <float> x; Vector <float> y; float * xPtr; float * yPtr; GetVectors(bytes, out x, out y, out xPtr, out yPtr); BLAS.Swap(x, y); Assert.IsTrue(AreEqual(1.3, x.Storage[0], delta)); Assert.IsTrue(AreEqual(1.4, x.Storage[1], delta)); Assert.IsTrue(AreEqual(1.1, y.Storage[1], delta)); Assert.IsTrue(AreEqual(1.2, y.Storage[4], delta)); BLAS.Swap(x.Descriptor, xPtr + x.Offset, y.Descriptor, yPtr + y.Offset); Assert.IsTrue(AreEqual(1.3, xPtr[0], delta)); Assert.IsTrue(AreEqual(1.4, xPtr[1], delta)); Assert.IsTrue(AreEqual(1.1, yPtr[1], delta)); Assert.IsTrue(AreEqual(1.2, yPtr[4], delta)); GetVectors(bytes, out x, out y, out xPtr, out yPtr); BLAS.Swap(y, x); Assert.IsTrue(AreEqual(1.3, x.Storage[0], delta)); Assert.IsTrue(AreEqual(1.4, x.Storage[1], delta)); Assert.IsTrue(AreEqual(1.1, y.Storage[1], delta)); Assert.IsTrue(AreEqual(1.2, y.Storage[4], delta)); BLAS.Swap(y.Descriptor, yPtr + y.Offset, x.Descriptor, xPtr + x.Offset); Assert.IsTrue(AreEqual(1.3, xPtr[0], delta)); Assert.IsTrue(AreEqual(1.4, xPtr[1], delta)); Assert.IsTrue(AreEqual(1.1, yPtr[1], delta)); Assert.IsTrue(AreEqual(1.2, yPtr[4], delta)); }
/// <summary> /// Returns the source which contains Au_{n-1}+b for the Operator and the field provided in the constructor /// </summary> /// <param name="k">results of Ay+b</param> /// <param name="dt">optional scaling by time step size</param> public void ExplicitEulerSource(double[] subgridCoordinates, out double[] convectiveSource, double dt) { convectiveSource = new double[subgridCoordinates.Length]; SubgridOperatorMatr.SpMVpara <double[], double[]>(dt, subgridCoordinates, 1.0, convectiveSource); BLAS.daxpy(SubgridAffine.Length, dt, SubgridAffine, 1, convectiveSource, 1); }
public void RotTests(float?value) { Vector <float> x; Vector <float> y; float * xPtr; float * yPtr; var c = 1.5f; var s = 1.6f; GetVectors(bytes, out x, out y, out xPtr, out yPtr); BLAS.Rotate(x, y, c, s); Assert.IsTrue(AreEqual(3.73, x.Storage[0], delta)); Assert.IsTrue(AreEqual(4.04, x.Storage[1], delta)); Assert.IsTrue(AreEqual(0.19, y.Storage[1], delta)); Assert.IsTrue(AreEqual(0.18, y.Storage[4], delta)); BLAS.Rotate(x.Descriptor, xPtr + x.Offset, y.Descriptor, yPtr + y.Offset, c, s); Assert.IsTrue(AreEqual(3.73, xPtr[0], delta)); Assert.IsTrue(AreEqual(4.04, xPtr[1], delta)); Assert.IsTrue(AreEqual(0.19, yPtr[1], delta)); Assert.IsTrue(AreEqual(0.18, yPtr[4], delta)); GetVectors(bytes, out x, out y, out xPtr, out yPtr); BLAS.Rotate(y, x, c, s); Assert.IsTrue(AreEqual(3.71, y.Storage[1], delta)); Assert.IsTrue(AreEqual(4.02, y.Storage[4], delta)); Assert.IsTrue(AreEqual(-0.43, x.Storage[0], delta)); Assert.IsTrue(AreEqual(-0.44, x.Storage[1], delta)); BLAS.Rotate(y.Descriptor, yPtr + y.Offset, x.Descriptor, xPtr + x.Offset, c, s); Assert.IsTrue(AreEqual(3.71, yPtr[1], delta)); Assert.IsTrue(AreEqual(4.02, yPtr[4], delta)); Assert.IsTrue(AreEqual(-0.43, xPtr[0], delta)); Assert.IsTrue(AreEqual(-0.44, xPtr[1], delta)); }
/// <summary> /// computes the inner product of this array and another array <paramref name="other"/>. /// </summary> /// <param name="other"> /// must be of the same size a this array /// </param> /// <returns></returns> public double InnerProduct(MultidimensionalArray other) { if (this.Dimension != other.Dimension) { throw new ArgumentException("mismatch in number of dimensions"); } int D = this.Dimension; for (int k = 0; k < D; k++) { if (this.GetLength(k) != other.GetLength(k)) { throw new ArgumentException("mismatch in dimension " + k); } } if (this.IsContinious && other.IsContinious) { unsafe { fixed(double *pStorageThis = this.m_Storage, pStorageOther = other.m_Storage) { return(BLAS.ddot(this.Length, pStorageThis, 1, pStorageOther, 1)); } } } else { double acc = 0; this.ApplyAll(delegate(int[] index, ref double entry) { acc += entry * other[index]; }); return(acc); } }
public void SDotTest() { Vector <float> x; Vector <float> y; float * xPtr; float * yPtr; GetVectors(bytes, out x, out y, out xPtr, out yPtr); double sdot = BLAS.SDot(1, x, y); Assert.AreEqual(4.11, sdot, delta); sdot = BLAS.SDot(1, x.Descriptor, xPtr + x.Offset, y.Descriptor, yPtr + y.Offset); Assert.AreEqual(4.11, sdot, delta); GetVectors(bytes, out x, out y, out xPtr, out yPtr); sdot = BLAS.SDot(1, y, x); Assert.AreEqual(4.11, sdot, delta); sdot = BLAS.SDot(1, y.Descriptor, yPtr + y.Offset, x.Descriptor, xPtr + x.Offset); Assert.AreEqual(4.11, sdot, delta); GetVectors(bytes, out x, out y, out xPtr, out yPtr); sdot = BLAS.SDot(x, y); Assert.AreEqual(3.11, sdot, delta); sdot = BLAS.SDot(x.Descriptor, xPtr + x.Offset, y.Descriptor, yPtr + y.Offset); Assert.AreEqual(3.11, sdot, delta); GetVectors(bytes, out x, out y, out xPtr, out yPtr); sdot = BLAS.SDot(y, x); Assert.AreEqual(3.11, sdot, delta); sdot = BLAS.SDot(y.Descriptor, yPtr + y.Offset, x.Descriptor, xPtr + x.Offset); Assert.AreEqual(3.11, sdot, delta); }
/// <summary> /// performs one Explicit Euler timestep /// </summary> /// <param name="dt">size of timestep</param> public virtual void Perform(double dt) { using (new ilPSP.Tracing.FuncTrace()) { double[] k = new double[m_SubgridMapping.subgridCoordinates.Length]; Evaluate(k, dt); BLAS.daxpy(DGCoordinates.Length, -1.0, k, 1, DGCoordinates, 1); } }
/// <summary> /// method for setting up the timestepper, i.e. the necessary /// </summary> protected void Setup1(Context ctx, ISparseSolver solver, bool[] temporalOp, MsrMatrix spatialOpMtx, IList <double> spatialOpAffine, SubGrid subgrid, SubgridCoordinateMapping fields, double InitialDeltat) { // check operator and arguments if (spatialOpMtx.NoOfRows != spatialOpMtx.NoOfCols) { throw new ArgumentException("matrix must be quadratic.", "spatialOpMtx"); } if (spatialOpMtx.NoOfRows != fields.GlobalCount) { throw new ArgumentException("matrix size must be equal to the GlobalCount of fields mapping", "fields,spatialOpMtx"); } if (spatialOpMtx.RowPartition.LocalLength != fields.NUpdate) { throw new ArgumentException("number of locally stored matrix rows nust be equal to NUpdate of fields mapping.", "fields,spatialOpMtx"); } if (spatialOpAffine.Count < fields.NUpdate) { throw new ArgumentException("length affine offset vector must be equal or larger than NUpdate of the mapping", "spatialOpAffine"); } m_Context = ctx; m_Solver = solver; m_Subgrid = subgrid; m_SubgridMapping = fields; m_AffineOffset1 = spatialOpAffine.ToArray(); this.temporalOp = temporalOp; BLAS.dscal(m_AffineOffset1.Length, -1.0, m_AffineOffset1, 1); { // check temporal operator // ----------------------- if (m_SubgridMapping.Fields.Count != temporalOp.Length) { throw new ArgumentException( "lenght of temporalOp must be equal to number of domain/codomain variables of the spatial differential operator", "temporalOp"); } m_SubgridMapping.Compress(); m_SubgridDGCoordinates = m_SubgridMapping.subgridCoordinates; m_SubgridMapping.SetupSubmatrix(m_AffineOffset1, spatialOpMtx, out m_CompressedAffine, out m_CompressedMatrix); bool timedep = false; bool fullyTimeDep = true; foreach (bool b in temporalOp) { timedep = timedep || b; fullyTimeDep = fullyTimeDep & b; } if (!timedep) { throw new ArgumentException("At least one equation must be time-dependent; one entry in temporalOp must be true;", "temporalOp"); } DefineMatrix(m_CompressedMatrix, InitialDeltat); } }
/// <summary> /// Computes the new intermediate DGCoordinates. /// Important: It does not change the DGCoordinates <see cref="ExplicitEuler.CurrentState"/>. /// </summary> /// <param name="completeChangeRate">Complete ChangeRate of a cluster for one sub-step</param> /// <returns>intermediate DGCoordinates as array</returns> protected virtual double[] ComputesUpdatedDGCoordinates(double[] completeChangeRate) { // Standard case: Just add completeChangeRate to DGCoordinates as array double[] upDGC = new double[Mapping.LocalLength]; CurrentState.CopyTo(upDGC, 0); upDGC = OrderValuesBySgrd(upDGC); BLAS.daxpy(upDGC.Length, -1, OrderValuesBySgrd(completeChangeRate), 1, upDGC, 1); return(upDGC); }
/// <summary> /// Perfoms the actual Adams-Bashforth time integration sub-step in a cluster /// </summary> protected virtual void MakeABStep() { using (new ilPSP.Tracing.FuncTrace("MakeABStep")) { CompleteChangeRate = new double[Mapping.LocalLength]; if (adaptive) { CompleteChangeRate = new double[Mapping.LocalLength]; for (int j = 0; j < ABSubGrid.LocalNoOfCells; j++) { int cell = jSub2jCell[j]; // cell = global cell index // f = each field // n = basis polynomial foreach (DGField f in Mapping.Fields) { for (int n = 0; n < f.Basis.GetLength(cell); n++) { int index = Mapping.LocalUniqueCoordinateIndex(f, cell, n); CompleteChangeRate[index] = CompleteChangeRate[index] + ABCoefficientsPerCell[j][0] * CurrentChangeRate[index]; int i = 1; foreach (double[] oldRate in HistoryChangeRate) { CompleteChangeRate[index] = CompleteChangeRate[index] + ABCoefficientsPerCell[j][i] * oldRate[index]; i++; } } } } } else { //y <-- alpha*x + y BLAS.daxpy(CompleteChangeRate.Length, ABCoefficients[0], CurrentChangeRate, 1, CompleteChangeRate, 1); BLAS.daxpy(CompleteBoundaryFluxes.Length, ABCoefficients[0], currentBndFluxes, 1, CompleteBoundaryFluxes, 1); // calculate completeChangeRate int i = 1; foreach (double[] oldRate in HistoryChangeRate) { BLAS.daxpy(CompleteChangeRate.Length, ABCoefficients[i], oldRate, 1, CompleteChangeRate, 1); i++; } i = 1; foreach (double[] oldRate in HistoryBoundaryFluxes) { BLAS.daxpy(CompleteBoundaryFluxes.Length, ABCoefficients[i], oldRate, 1, CompleteBoundaryFluxes, 1); i++; } } } }
public void RotmTest(float?value) { Vector <float> x; Vector <float> y; float * xPtr; float * yPtr; // Rotate foreach (var h in new[] { new[] { 1.5f, 1.6f, 1.7f, 1.8f }, new[] { 1f, 0f, 0f, 1f }, new[] { 1f, 1.5f, 1.6f, 1f }, new[] { 1.5f, 1f, -1f, 1.6f } }) { var h11 = h[0]; var h12 = h[1]; var h21 = h[2]; var h22 = h[3]; GetVectors(bytes, out x, out y, out xPtr, out yPtr); BLAS.Rotate(x, y, h11, h12, h21, h22); Assert.IsTrue(AreEqual(1.1 * h11 + 1.3 * h12, x.Storage[0], delta)); Assert.IsTrue(AreEqual(1.2 * h11 + 1.4 * h12, x.Storage[1], delta)); Assert.IsTrue(AreEqual(1.1 * h21 + 1.3 * h22, y.Storage[1], delta)); Assert.IsTrue(AreEqual(1.2 * h21 + 1.4 * h22, y.Storage[4], delta)); BLAS.Rotate( x.Descriptor, xPtr + x.Offset, y.Descriptor, yPtr + y.Offset, h11, h12, h21, h22); Assert.IsTrue(AreEqual(1.1 * h11 + 1.3 * h12, xPtr[0], delta)); Assert.IsTrue(AreEqual(1.2 * h11 + 1.4 * h12, xPtr[1], delta)); Assert.IsTrue(AreEqual(1.1 * h21 + 1.3 * h22, yPtr[1], delta)); Assert.IsTrue(AreEqual(1.2 * h21 + 1.4 * h22, yPtr[4], delta)); GetVectors(bytes, out x, out y, out xPtr, out yPtr); BLAS.Rotate(y, x, h11, h12, h21, h22); Assert.IsTrue(AreEqual(1.3 * h11 + 1.1 * h12, y.Storage[1], delta)); Assert.IsTrue(AreEqual(1.4 * h11 + 1.2 * h12, y.Storage[4], delta)); Assert.IsTrue(AreEqual(1.3 * h21 + 1.1 * h22, x.Storage[0], delta)); Assert.IsTrue(AreEqual(1.4 * h21 + 1.2 * h22, x.Storage[1], delta)); BLAS.Rotate( y.Descriptor, yPtr + y.Offset, x.Descriptor, xPtr + x.Offset, h11, h12, h21, h22); Assert.IsTrue(AreEqual(1.3 * h11 + 1.1 * h12, yPtr[1], delta)); Assert.IsTrue(AreEqual(1.4 * h11 + 1.2 * h12, yPtr[4], delta)); Assert.IsTrue(AreEqual(1.3 * h21 + 1.1 * h22, xPtr[0], delta)); Assert.IsTrue(AreEqual(1.4 * h21 + 1.2 * h22, xPtr[1], delta)); } }
/// <summary> /// multiplies all entries by number <paramref name="a"/> /// </summary> /// <param name="a"></param> public void Scale(double a) { BLAS.dscal(m_BaseStorage.Length, a, m_BaseStorage, 1); for (int j = m_ExtendedStorage.Length - 1; j >= 0; j--) { double[] st = m_ExtendedStorage[j]; if (st != null) { BLAS.dscal(st.Length, a, st, 1); } } }
/// <summary> /// performs one timestep /// </summary> /// <param name="dt">size of timestep</param> public override double Perform(double dt) { using (var tr = new ilPSP.Tracing.FuncTrace()) { if (TimeStepConstraints != null) { dt = CalculateTimeStep(); } double[][] k = new double[m_Scheme.Stages][]; for (int i = 0; i < m_Scheme.Stages; i++) { k[i] = new double[Mapping.LocalLength]; } double[] y0 = new double[Mapping.LocalLength]; CurrentState.CopyTo(y0, 0); // logging tr.Info("Runge-Kutta Scheme with " + m_Scheme.Stages + " stages."); double time0 = m_Time; tr.Info("time = " + time0 + ", dt = " + dt); // berechne k[0] ComputeChangeRate(k[0], m_Time, 0);// invokes MPI communication for (int s = 1; s < m_Scheme.Stages; s++) { PerformStage(y0, s, k, dt); m_Time = time0 + m_Scheme.c[s] * dt; ComputeChangeRate(k[s], m_Time, m_Scheme.c[s] * dt);// invokes MPI communication } // next timestep CurrentState.Clear(); CurrentState.CopyFrom(y0, 0); Array.Clear(y0, 0, y0.Length); for (int s = 0; s < m_Scheme.Stages; s++) { if (m_Scheme.b[s] != 0.0) { BLAS.daxpy(y0.Length, -m_Scheme.b[s] * dt, k[s], 1, y0, 1); } } CurrentState.axpy <double[]>(y0, 1.0); base.ApplyFilter(dt); m_Time = time0 + dt; } return(dt); }
private static void DetailCheckSetup() { int n = 50; int spacing = 1; double[] x = new double[n]; double[] b = new double[n]; MultidimensionalArray M = MultidimensionalArray.Create(n, n); // create some array for (int i = 0; i < n; i++) { b[i] = 1; for (int j = 0; j < n; j++) { M[i, j] = i + Math.Pow(j, i); } } try { Console.Write("Starting with a test of LAPACK Function DGETRF\n"); M.Solve(x, b); Console.Write("Succesfully called DGETRF\n\n"); Console.Write("Starting with a test of BLAS Function DDOT\n"); BLAS.DDOT(ref n, x, ref spacing, b, ref spacing); Console.Write("Succesfully called DDOT\n\n"); Console.Write("Starting with a test of Tecplot Function tecnod110\n"); Console.Write("This test is not yet implemented\n"); Console.Write("Succesfully called Tecplot Function tecnod110\n\n"); Console.Write("Starting with a test of METIS Function \n"); Console.Write("This test is not yet implemented\n"); Console.Write("Succesfully called METIS \n\n"); Console.Write("Starting with a test of the MUMPS Solver\n"); Console.Write("This test is not yet implemented\n"); Console.Write("Succesfully called the MUMPS Solver\n\n"); Console.Write("Starting with a test of the PARDISO Solver\n"); Console.Write("This test is not yet implemented\n"); Console.Write("Succesfully called the PARDISO Solver\n\n"); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Performs a single stage of a timestep. /// </summary> /// <param name="y0">Initial DG coordinates</param> /// <param name="s">The current stage</param> /// <param name="k">The current change rate</param> /// <param name="dt">The size of the timestep</param> protected virtual void PerformStage(double[] y0, int s, double[][] k, double dt) { Array.Clear(m_DGCoordinates, 0, DGCoordinates.Length); //m_DGCoordinates.axpy<double[]>(y0, 1.0); BLAS.daxpy(m_DGCoordinates.Length, 1.0, y0, 1, m_DGCoordinates, 1); for (int r = 0; r < s; r++) { if (m_RKscheme.a[s, r] != 0.0) { //m_DGCoordinates.axpy<double[]>(k[r], -dt * m_Scheme.a[s, r]); BLAS.daxpy(m_DGCoordinates.Length, -dt * m_RKscheme.a[s, r], k[r], 1, m_DGCoordinates, 1); } } }
static void Main(string[] args) { int num_threads = 2; BLAS.OpenblasSetNumThreads(ref num_threads); float[,] a = new float[, ] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; float[,] b = new float[, ] { { 2, 2, 2 }, { 3, 3, 3 } }; var c = Dot(a, b, 1, 0); Console.ReadLine(); }
/// <summary> /// multiplies all entries of this array by <paramref name="alpha"/> /// </summary> /// <param name="alpha"></param> public void Scale(double alpha) { if (this.IsContinious) { unsafe { fixed(double *pStorage = this.m_Storage) { BLAS.dscal(this.Length, alpha, pStorage + this.m_Offset, 1); } } } else { ApplyAll(d => d * alpha); } }
/// <summary> /// assembles the right-hand-side (DG coordinates of <paramref name="rhsIn"/> minus the affine offset <see cref="m_AffineOffset"/> /// of the operator, which usually carries the boundary conditions) /// </summary> /// <param name="rhsIn">input</param> /// <param name="TotalRhs">output</param> void GetTotalRHS(IList <double> rhsIn, out double[] TotalRhs) { int n = m_Mapping.LocalLength; // check if (rhsIn != null) { if (rhsIn.Count < m_Mapping.LocalLength) { throw new ArgumentOutOfRangeException("right hand side vector to short."); } } // clone or allocate memory if (rhsIn == null) { TotalRhs = new double[m_AffineOffset.Length]; } else { TotalRhs = rhsIn as double[]; if (TotalRhs == null) { TotalRhs = rhsIn.ToArray(); } } rhsIn = null; //Console.WriteLine("Affine offset norm = " + BLAS.dnrm2(m_AffineOffset.Length,m_AffineOffset,1)); // call evaluator if (m_rhsEvaluator != null) { m_rhsEvaluator.Evaluate <double[]>(1.0, 1.0, TotalRhs); } // notify if (RHSEvaluated != null) { RHSEvaluated(TotalRhs); } // substract affine part BLAS.daxpy(n, -1.0, m_AffineOffset, 1, TotalRhs, 1); }
double MinimizeResidual(double[] outX, double[] Sol0, double[] Res0, double[] outRes, bool diagnosis = false) { using (new FuncTrace()) { Debug.Assert(SolHistory.Count == MxxHistory.Count); Debug.Assert(outX.Length == m_MgOperator.Mapping.LocalLength); Debug.Assert(Sol0.Length == m_MgOperator.Mapping.LocalLength); Debug.Assert(Res0.Length == m_MgOperator.Mapping.LocalLength); Debug.Assert(outRes.Length == m_MgOperator.Mapping.LocalLength); int KrylovDim = SolHistory.Count; int L = outX.Length; double[] alpha = new double[KrylovDim]; for (int i = 0; i < KrylovDim; i++) { //alpha[i] = GenericBlas.InnerProd(MxxHistory[i], Res0).MPISum(); alpha[i] = BLAS.ddot(L, MxxHistory[i], 1, Res0, 1); } alpha = alpha.MPISum(); Array.Copy(Sol0, outX, L); Array.Copy(Res0, outRes, L); for (int i = 0; i < KrylovDim; i++) { //outX.AccV(alpha[i], SolHistory[i]); //outRes.AccV(-alpha[i], MxxHistory[i]); BLAS.daxpy(L, alpha[i], SolHistory[i], 1, outX, 1); BLAS.daxpy(L, -alpha[i], MxxHistory[i], 1, outRes, 1); } double ResNorm = BLAS.dnrm2(L, outRes, 1).Pow2().MPISum().Sqrt(); //Console.WriteLine("OrthonormalizationMultigrid: minimizing ofer " + KrylovDim + " vectors"); return(ResNorm); /* we cannot do the following * // since the 'MxxHistory' vectors form an orthonormal system, * // the L2-norm is the L2-Norm of the 'alpha'-coordinates (Parceval's equality) * return alpha.L2Norm(); */ } }
private List <PointOnGrid> ComputeProjection(StateVector stateVec, List <PointOnGrid> origPoints) { /* * rotation_matrix = yaw_rotation_matrix * pitch_rotation_matrix * roll_rotation_matrix; * world_matrix = translation_matrix * rotation_matrix; */ List <PointOnGrid> projPoints = new List <PointOnGrid>(); double[,] T_mat = Transltions.CreateTranslationMatrix(new double[] { stateVec.XCG, stateVec.YCG, stateVec.ZCG }); double[,] R_Gen = Rotations.CreateRotationThetaPsiPhi(Rotations.ToRadians(stateVec.PitchAngle), Rotations.ToRadians(stateVec.YawAngle), Rotations.ToRadians(stateVec.RollAngle)); double[,] World_mat = BLAS.Multiply(T_mat, R_Gen); /* * [u,v]= Cam_Project_mat*world_matrix*[x y z] */ double[,] Cam_Project_mat = Camera.CreateProjectionMatrix(); double[,] Launch_to_Camera = Camera.CreateAxisTranformMatrix(); double[,] FinalMat = BLAS.Multiply(Cam_Project_mat, BLAS.Multiply(Launch_to_Camera, World_mat)); for (int ii = 0; ii < origPoints.Count; ii++) { // Apply transformation to original position double[] vec = new double[] { origPoints[ii].X, origPoints[ii].Y, origPoints[ii].Z, 1 }; double[] transf_camera = BLAS.Multiply(FinalMat, vec); transf_camera[0] /= transf_camera[2]; transf_camera[1] /= transf_camera[2]; transf_camera[2] /= transf_camera[2]; //double[] transfScaled = BLAS.Multiply(Scales.ScaleMat, transf_camera); if (transf_camera[0] > 0 && transf_camera[1] > 0 && transf_camera[0] < Camera.CameraSettings.SensorWidth && transf_camera[1] < Camera.CameraSettings.SensorHeight) { projPoints.Add(new PointOnGrid((float)transf_camera[0], (float)transf_camera[1], 0, ii, origPoints[ii].Intensity, origPoints[ii].Component)); } } return(projPoints); }
public void Norm2Test(float?value) { Vector <float> x; Vector <float> y; float * xPtr; float * yPtr; GetVectors(bytes, out x, out y, out xPtr, out yPtr); float norm = BLAS.Norm2(x); Assert.IsTrue(AreEqual(1.6279, norm, delta)); norm = BLAS.Norm2(x.Descriptor, xPtr + x.Offset); Assert.IsTrue(AreEqual(1.6279, norm, delta)); norm = BLAS.Norm2(y); Assert.IsTrue(AreEqual(1.9105, norm, delta)); norm = BLAS.Norm2(y.Descriptor, yPtr + y.Offset); Assert.IsTrue(AreEqual(1.9105, norm, delta)); }
public void ASumTest(float?value) { Vector <float> x; Vector <float> y; float * xPtr; float * yPtr; GetVectors(bytes, out x, out y, out xPtr, out yPtr); var sum = BLAS.ASum(x); Assert.IsTrue(AreEqual(2.3, sum, delta)); sum = BLAS.ASum(x.Descriptor, xPtr); Assert.IsTrue(AreEqual(2.3, sum, delta)); sum = BLAS.ASum(y); Assert.IsTrue(AreEqual(2.7, sum, delta)); sum = BLAS.ASum(y.Descriptor, yPtr + y.Offset); Assert.IsTrue(AreEqual(2.7, sum, delta)); }
static Array Dot(float[,] a, float[,] b, float alpha, float beta) { unsafe { int m = 2; int k = 3; int n = 2; int lda = 2; int ldb = 3; int ldc = 2; float[,] c = new float[m, n]; sbyte nta = (sbyte)BlasOp.NonTranspose; BLAS.Sgemm(&nta, &nta, ref m, ref n, ref k, ref alpha, ref a[0, 0], ref lda, ref b[0, 0], ref ldb, ref beta, ref c[0, 0], ref ldc); return(c); } }
public void DotUTest(complexf?value) { Vector <complexf> x; Vector <complexf> y; complexf * xPtr; complexf * yPtr; complexf i = complexf.ImaginaryOne; GetComplexVectors(bytes, out x, out y, out xPtr, out yPtr); complexf dot = BLAS.Dot(x, y); Assert.IsTrue(AreEqual(-0.58 + 8.28 * i, dot, delta)); dot = BLAS.Dot(x.Descriptor, xPtr + x.Offset, y.Descriptor, yPtr + y.Offset); Assert.IsTrue(AreEqual(-0.58 + 8.28 * i, dot, delta)); dot = BLAS.Dot(y, x); Assert.IsTrue(AreEqual(-0.58 + 8.28 * i, dot, delta)); dot = BLAS.Dot(y.Descriptor, yPtr + y.Offset, x.Descriptor, xPtr + x.Offset); Assert.IsTrue(AreEqual(-0.58 + 8.28 * i, dot, delta)); }
/// <summary> /// performs one timestep /// </summary> /// <param name="dt">size of timestep</param> public override void Perform(double dt) { using (var tr = new ilPSP.Tracing.FuncTrace()) { double[][] k = new double[m_RKscheme.Stages][]; for (int i = 0; i < m_RKscheme.Stages; i++) { k[i] = new double[m_SubgridMapping.subgridCoordinates.Length]; } double[] y0 = new double[m_SubgridMapping.subgridCoordinates.Length]; m_DGCoordinates.CopyTo(y0, 0); // logging tr.Info("Runge-Kutta Scheme with " + m_RKscheme.Stages + " stages."); double time0 = m_Time; tr.Info("time = " + time0 + ", dt = " + dt); // berechne k[0] Evaluate(k[0], m_Time);// invokes MPI communication for (int s = 1; s < m_RKscheme.Stages; s++) { PerformStage(y0, s, k, dt); m_Time = time0 + m_RKscheme.c[s] * dt; //ComputeChangeRate(k[s], m_Time, m_RKscheme.c[s] * dt);// invokes MPI communication Evaluate(k[s], m_Time); } // next timestep y0.CopyTo(m_DGCoordinates, 0); Array.Clear(y0, 0, y0.Length); for (int s = 0; s < m_RKscheme.Stages; s++) { BLAS.daxpy(y0.Length, -m_RKscheme.b[s] * dt, k[s], 1, y0, 1); } //m_DGCoordinates.axpy<double[]>(y0, 1.0); BLAS.daxpy(m_DGCoordinates.Length, 1.0, y0, 1, m_DGCoordinates, 1); m_Time = time0 + dt; } }
public void DotTest(float?value) { Vector <float> x; Vector <float> y; float * xPtr; float * yPtr; GetVectors(bytes, out x, out y, out xPtr, out yPtr); var dot = BLAS.Dot(x, y); Assert.AreEqual(3.11, dot, delta); dot = BLAS.Dot(x.Descriptor, xPtr + x.Offset, y.Descriptor, yPtr + y.Offset); Assert.AreEqual(3.11, dot, delta); GetVectors(bytes, out x, out y, out xPtr, out yPtr); dot = BLAS.Dot(y, x); Assert.AreEqual(3.11, dot, delta); dot = BLAS.Dot(y.Descriptor, yPtr + y.Offset, x.Descriptor, xPtr + x.Offset); Assert.AreEqual(3.11, dot, delta); }
public void IAMinTest(float?value) { Vector <float> x; Vector <float> y; float * xPtr; float * yPtr; GetVectors(bytes, out x, out y, out xPtr, out yPtr); Assert.AreEqual(0, BLAS.IAMin(x)); Assert.AreEqual(0, BLAS.IAMin(y)); x.Storage[0] = 3; y.Storage[1] = 3; Assert.AreEqual(1, BLAS.IAMin(y)); Assert.AreEqual(1, BLAS.IAMin(x)); Assert.AreEqual(0, BLAS.IAMin(x.Descriptor, xPtr + x.Offset)); Assert.AreEqual(0, BLAS.IAMin(y.Descriptor, yPtr + y.Offset)); xPtr[0] = 3; yPtr[1] = 3; Assert.AreEqual(1, BLAS.IAMin(x.Descriptor, xPtr + x.Offset)); Assert.AreEqual(1, BLAS.IAMin(y.Descriptor, yPtr + y.Offset)); }
public void ScalTest(float?value) { Vector <float> x; Vector <float> y; float * xPtr; float * yPtr; float alpha = 1.5f; GetVectors(bytes, out x, out y, out xPtr, out yPtr); BLAS.Scale(alpha, x); Assert.IsTrue(AreEqual(1.1 * alpha, x.Storage[0], delta)); Assert.IsTrue(AreEqual(1.2 * alpha, x.Storage[1], delta)); BLAS.Scale(alpha, x.Descriptor, xPtr + x.Offset); Assert.IsTrue(AreEqual(1.1 * alpha, xPtr[0], delta)); Assert.IsTrue(AreEqual(1.2 * alpha, xPtr[1], delta)); BLAS.Scale(alpha, y); Assert.IsTrue(AreEqual(1.3 * alpha, y.Storage[1], delta)); Assert.IsTrue(AreEqual(1.4 * alpha, y.Storage[4], delta)); BLAS.Scale(alpha, y.Descriptor, yPtr + y.Offset); Assert.IsTrue(AreEqual(1.3 * alpha, yPtr[1], delta)); Assert.IsTrue(AreEqual(1.4 * alpha, yPtr[4], delta)); }