public VectorOps(MyWorkingNode caller, VectorOperation operations, MyMemoryBlock<float> tempBlock) { m_caller = caller; m_operations = operations; m_temp = tempBlock; MatOperation mat_ops = MatOperation.None; if (m_operations.HasFlag(VectorOperation.Rotate)) { Debug.Assert(tempBlock.Count >= 4, "Temporary memory block has to be large at least 4 items when using Rotate operation"); mat_ops |= MatOperation.Multiplication; } if (m_operations.HasFlag(VectorOperation.Angle)) mat_ops |= MatOperation.DotProd; if (m_operations.HasFlag(VectorOperation.DirectedAngle)) { mat_ops |= MatOperation.Multiplication | MatOperation.DotProd; m_operations |= VectorOperation.Angle | VectorOperation.Rotate; } m_matOperation = new MyMatrixAutoOps(caller, mat_ops); }
private static NdArray <T> Dot2x2 <T>(IBufferNdArrayImpl <T> x, IBufferNdArrayImpl <T> yT) { // x .Shape = [m, p] // yT.Shape = [n, p] // retval.Shape = [m, n] var p = x.Shape[1]; Guard.AssertShapeMatch( p == yT.Shape[1], $"x.Shape[1] = {p}, y.Shape[0] = {yT.Shape[1]}"); var m = x.Shape[0]; var n = yT.Shape[0]; var resImpl = new RawNdArrayImpl <T>(new IndexArray(m, n)); var xBuff = x.Buffer; var yTBuff = yT.Buffer; var resBuff = resImpl.Buffer.Span; for (var i = 0; i < m; ++i) { for (var j = 0; j < n; ++j) { resBuff[n * i + j] = VectorOperation.Dot(xBuff.Slice(i * p, p), yTBuff.Slice(j * p, p)); } } return(new NdArray <T>(resImpl)); }
public override void UnaryElementWiseOperation(NArray <double> a, NArray <double> result, UnaryElementWiseOperation operation) { if (operation == VectorAccelerator.UnaryElementWiseOperation.Negate) { ScaleOffset(a, -1, 0, result); return; } VectorOperation vectorVectorOperation = null; switch (operation) { case VectorAccelerator.UnaryElementWiseOperation.CumulativeNormal: vectorVectorOperation = IntelMathKernelLibrary.CumulativeNormal; break; case VectorAccelerator.UnaryElementWiseOperation.Exp: vectorVectorOperation = IntelMathKernelLibrary.Exp; break; case VectorAccelerator.UnaryElementWiseOperation.InverseCumulativeNormal: vectorVectorOperation = IntelMathKernelLibrary.InverseCumulativeNormal; break; case VectorAccelerator.UnaryElementWiseOperation.InverseSquareRoot: vectorVectorOperation = IntelMathKernelLibrary.InverseSquareRoot; break; case VectorAccelerator.UnaryElementWiseOperation.Inverse: vectorVectorOperation = IntelMathKernelLibrary.Inverse; break; case VectorAccelerator.UnaryElementWiseOperation.Log: vectorVectorOperation = IntelMathKernelLibrary.Log; break; case VectorAccelerator.UnaryElementWiseOperation.SquareRoot: vectorVectorOperation = IntelMathKernelLibrary.SquareRoot; break; } VectorOperation(a, result, vectorVectorOperation); }
public VectorOps(MyWorkingNode caller, VectorOperation operations, MyMemoryBlock <float> tempBlock) { m_caller = caller; m_operations = operations; m_temp = tempBlock; MatOperation mat_ops = MatOperation.None; if (m_operations.HasFlag(VectorOperation.Rotate)) { Debug.Assert(tempBlock.Count >= 4, "Temporary memory block has to be large at least 4 items when using Rotate operation"); mat_ops |= MatOperation.Multiplication; } if (m_operations.HasFlag(VectorOperation.Angle)) { mat_ops |= MatOperation.DotProd; } if (m_operations.HasFlag(VectorOperation.DirectedAngle)) { mat_ops |= MatOperation.Multiplication | MatOperation.DotProd; m_operations |= VectorOperation.Angle | VectorOperation.Rotate; } m_matOperation = new MyMatrixAutoOps(caller, mat_ops); }
private void VectorOperation(NArray <double> a, NArray <double> result, VectorOperation operation) { double[] aArray, resultArray; int aStart, resultStart; GetArray(a, out aArray, out aStart); GetArray(result, out resultArray, out resultStart); operation(aArray, aStart, resultArray, resultStart, result.Length); }
public double[] AddSimd() { var array = new double[_length]; var simdOp = VectorOperation.Simdize <double>((a, b, c, d, e) => a + b + c + d + e); for (var j = 0; j < _times; ++j) { simdOp(A, B, C, D, E, array); } return(array); }
private static NdArray <T> Dot1x1 <T>(IBufferNdArrayImpl <T> x, IBufferNdArrayImpl <T> y) { // x.Shape = [p] // y.Shape = [p] // retval.Shape = [1] var p = x.Shape[0]; Guard.AssertShapeMatch(p == y.Shape[0], $"x.Shape[0] = {p}, y.Shape[0] = {y.Shape[0]}"); var resImpl = new RawNdArrayImpl <T>(new IndexArray(1)); var xBuff = x.Buffer; var yBuff = y.Buffer; var resBuff = resImpl.Buffer.Span; resBuff[0] = VectorOperation.Dot(xBuff, yBuff); return(new NdArray <T>(resImpl)); }
public static BaseMathEntity CalculateVector(VectorOperation operation, BaseMathEntity a, BaseMathEntity b) { switch (operation.Operation) { case VectorOperationEnum.Add: return(Round(a.Add(b))); case VectorOperationEnum.Subtract: return(Round(a.Subtract(b))); case VectorOperationEnum.Multiply: return(Round(a.Multiply(b))); case VectorOperationEnum.MatrixMupltiply: return(Round(a.MatrixMultiply(b))); } return(null); }
private bool Validate(VectorOperation operation, int sizeA, int sizeB) { if (operation == VectorOperation.None) { return(false); } if ((operation & m_operations) == 0) { MyLog.WARNING.WriteLine("Trying to execute an uninitialized vector operation. Owner: " + m_caller.Name); return(false); } if (operation != VectorOperation.Rotate && sizeA != sizeB) { MyLog.ERROR.WriteLine("Vectors have to be the same length for this operation. Owner: " + m_caller.Name); return(false); } return(true); }
// reference: // https://github.com/xianyi/OpenBLAS/blob/develop/reference/dlaswpf.f /// <summary> /// [dlaswp] Swaps rows on the matrix A. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="a"></param> /// <param name="colstart"></param> /// <param name="collength"></param> /// <param name="ipiv"></param> /// <remarks> The columns between <c>[colstart, colstart + collength)</c> </remarks> public static void SwapRows <T>(MutableNdArray <T> a, int colstart, int collength, ReadOnlySpan <int> ipiv) { Guard.AssertArgumentRange(0 <= colstart, "0 <= colstart"); Guard.AssertArgumentRange(colstart + collength < a.Shape[1], "colstart + collength < a.Shape[1]"); if (a is RawNdArray <T> xa) { using var alloc = new AllocationSlim <T>(collength); var tmp = alloc.Memory.Slice(0, collength); var c = a.Shape[1]; for (var i = 0; i < ipiv.Length; ++i) { if (i == ipiv[i]) { continue; } var row1 = xa.Entity.Buffer.Slice(c * ipiv[i] + colstart, collength); var row2 = xa.Entity.Buffer.Slice(c * i + colstart, collength); VectorOperation.Identity(row1, tmp); VectorOperation.Identity(row2, row1); VectorOperation.Identity(tmp, row2); } } else { for (var i = 0; i < ipiv.Length; ++i) { if (i == ipiv[i]) { continue; } for (var j = colstart; j < colstart + collength; ++j) { var tmp = a[i, j]; a[i, j] = a[ipiv[i], j]; a[ipiv[i], j] = tmp; } } } }
private static NdArray <T> Dot2x1 <T>(IBufferNdArrayImpl <T> x, IBufferNdArrayImpl <T> y) { // x.Shape = [m, p] // y.Shape = [p] // retval.Shape = [m] var p = x.Shape[1]; Guard.AssertShapeMatch(p == y.Shape[0], $"x.Shape[1] = {p}, y.Shape[0] = {y.Shape[0]}"); var m = x.Shape[0]; var resImpl = new RawNdArrayImpl <T>(new IndexArray(m)); var xBuff = x.Buffer; var yBuff = y.Buffer; var resBuff = resImpl.Buffer.Span; for (var i = 0; i < m; ++i) { resBuff[i] = VectorOperation.Dot(xBuff.Slice(i * p, p), yBuff); } return(new NdArray <T>(resImpl)); }
// reference: // https://github.com/xianyi/OpenBLAS/blob/develop/reference/dtrsmf.f /// <summary> /// [dtrsm] Solves one of the matrix equations: /// <list type="bullet"> /// <item> /// <term><c>(side, transa) = (Left, None)</c></term> /// <description><c>A * X = alpha * B</c></description> /// </item> /// <item> /// <term><c>(side, transa) = (Right, None)</c></term> /// <description><c>X * A = alpha * B</c></description> /// </item> /// </list> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="side"></param> /// <param name="uplo"></param> /// <param name="diag"> <c>true</c> if <paramref name="a"/> is a unit triangular; otherwise, <c>false</c>. </param> /// <param name="alpha"></param> /// <param name="a"></param> /// <param name="b"></param> public static void SolveTriangleMatrix <T>(OperandSide side, TriangleKind uplo, bool diag, T alpha, INdArray <T> a, MutableNdArray <T> b) { if (ValueTrait.Equals(alpha, Zero <T>())) { VectorOperation.Identity(NdArray.Zeros <T>(b.Shape), b); return; } switch ((side, uplo)) { case (OperandSide.Left, TriangleKind.Upper): SolveTriangleMatrixLU(diag, alpha, a, b); break; case (OperandSide.Left, TriangleKind.Lower): SolveTriangleMatrixLL(diag, alpha, a, b); break; case (OperandSide.Right, TriangleKind.Upper): SolveTriangleMatrixRU(diag, alpha, a, b); break; case (OperandSide.Right, TriangleKind.Lower): SolveTriangleMatrixRL(diag, alpha, a, b); break; default: Guard.ThrowArgumentError("Invalid configs."); break; } }
private void SetClass <T>(T cls, Type type) { if (type == BoundType) { boundCalculator = cls as BoundCalculator; } else if (type == CoroutineType) { coroutine = cls as Coroutine; } else if (type == MeshOperationType) { meshOperation = cls as MeshOperation; } else if (type == ObjectPoolType) { objectPool = cls as ObjectPool; } else if (type == VectorOperationType) { vectorOperation = cls as VectorOperation; } }
public void SimdizeInt2(int length, Expression <Func <int, int, int> > expr) { var random = new System.Random(1234); var x = Enumerable.Range(0, length).Select(_ => random.Next()).ToArray(); var y = Enumerable.Range(0, length).Select(_ => random.Next()).ToArray(); var func = expr.Compile(); var expected = x.Zip(y, func).ToArray(); // 1st time: compile expression // 2nd or later: load from cache for (var i = 0; i < 8; ++i) { var simdFunc = VectorOperation.Simdize(expr); var actual = new int[x.Length]; simdFunc(x, y, actual); Assert.Equal(expected.Length, actual.Length); for (var j = 0; j < expected.Length; ++j) { Assert.Equal(expected[j], actual[j]); } } }
public void Run(VectorOperation operation, MyMemoryBlock <float> a, MyMemoryBlock <float> b, MyMemoryBlock <float> result) { if (!Validate(operation, a.Count, b.Count)) { return; } switch (operation) { case VectorOperation.Rotate: { b.SafeCopyToHost(); float rads = DegreeToRadian(b.Host[0]); float[] transform = { (float)Math.Cos(rads), -(float)Math.Sin(rads), (float)Math.Sin(rads), (float)Math.Cos(rads) }; Array.Copy(transform, m_temp.Host, transform.Length); m_temp.SafeCopyToDevice(); m_matOperation.Run(MatOperation.Multiplication, m_temp, a, result); } break; case VectorOperation.Angle: { m_matOperation.Run(MatOperation.DotProd, a, b, result); result.SafeCopyToHost(); float dotProd = result.Host[0]; float angle = RadianToDegree((float)Math.Acos(dotProd)); result.Fill(0); result.Host[0] = angle; result.SafeCopyToDevice(); } break; case VectorOperation.DirectedAngle: { result.Host[0] = -90; result.SafeCopyToDevice(); Run(VectorOperation.Rotate, a, result, result); result.CopyToMemoryBlock(m_temp, 0, 0, result.Count); m_matOperation.Run(MatOperation.DotProd, a, b, result); result.SafeCopyToHost(); float dotProd = result.Host[0]; float angle; if (Math.Abs(Math.Abs(dotProd) - 1) < 1E-4) { angle = 0; } else { angle = RadianToDegree((float)Math.Acos(dotProd)); } m_matOperation.Run(MatOperation.DotProd, m_temp, b, result); result.SafeCopyToHost(); float perpDotProd = result.Host[0]; if (perpDotProd > 0) { angle *= -1; } result.Fill(0); result.Host[0] = angle; result.SafeCopyToDevice(); } break; } }
private void butOk_Click(object sender, EventArgs e) { if (this.CheckingInput()) { Application.DoEvents(); this.comboxlayer.Enabled = false; this.comboxFiled.Enabled = false; this.comboClss1.Enabled = false; this.comboClss2.Enabled = false; this.textBox1.Enabled = false; this.button1.Enabled = false; this.butOk.Enabled = false; this.progressBar1.Visible = true; this.progressBar1.Minimum = 0; ILayer layer = this.MapLayer[this.comboxlayer.SelectedIndex]; IDataset pDataSet = (layer as IFeatureLayer).FeatureClass as IDataset; //string outputDir = System.Environment.CurrentDirectory + "\\Default.gdb"; IWorkspace pWorkspace = Utilities.WorkspaceHelper.GetShapefileWorkspace(MainForm.outshape); // IEnumDataset pDataSetsEnum = pWorkspace.get_Datasets(esriDatasetType.esriDTAny); IDataset pDataTM = pDataSetsEnum.Next(); while (pDataTM != null) { if (pDataTM.Name == "temp") { if (pDataTM.CanDelete()) { pDataTM.Delete(); break; } else { MessageBox.Show("请检查是否处于打开状态!"); return; } } pDataTM = pDataSetsEnum.Next(); } ArcGISUtilities.CopyData((layer as IFeatureLayer).FeatureClass, pWorkspace, "temp"); IEnumDataset pEnumDS = pWorkspace.get_Datasets(esriDatasetType.esriDTAny); IFeatureClass featureClass = ArcGISUtilities.GetDatasetByName(pEnumDS, "temp") as IFeatureClass; IQueryFilter pQueryFilter = null; IFeatureCursor pFCursor = null; int index = featureClass.Fields.FindField(comboxFiled.Text); if (index < 0) { MessageBox.Show("请检查数据是否含分类码或检查编码文件.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } foreach (ListViewItem lvi in listView1.Items) { string code = lvi.Text; if (code.Substring(0, 1) == "0" || code.Substring(0, 1) == "1") { string temp = lvi.Text.Substring(2, 2); //一级类 if (temp == "00") { code = code.Substring(0, 2); pQueryFilter = new QueryFilterClass(); //pQueryFilter.WhereClause = "[" + comboxFiled.Text + "]" + " LIKE " + "\'" + code + "**" + "\'";//GeoDatase中 pQueryFilter.WhereClause = comboxFiled.Text + " LIKE " + "\'" + code + "__" + "\'"; pFCursor = featureClass.Update(pQueryFilter, false); IFeature pFeat = pFCursor.NextFeature(); while (pFeat != null) { pFeat.set_Value(index, lvi.Text); pFCursor.UpdateFeature(pFeat); pFeat = pFCursor.NextFeature(); } } //二级类 else if (temp.Substring(1, 1) == "0") { code = code.Substring(0, 3); pQueryFilter = new QueryFilterClass(); pQueryFilter.WhereClause = "[" + comboxFiled.Text + "]" + " LIKE " + "\'" + code + "*" + "\'"; pFCursor = featureClass.Update(pQueryFilter, false); IFeature pFeat = pFCursor.NextFeature(); while (pFeat != null) { pFeat.set_Value(index, lvi.Text); pFCursor.UpdateFeature(pFeat); pFeat = pFCursor.NextFeature(); } } } this.progressBar1.Value += 1; } //////////////////////////////////////////////////////////////////////////////////////////////////// Application.DoEvents(); this.progressBar1.Value = 70; IFeatureClass pFeatureClass = null; VectorOperation operation = new VectorOperation(); pFeatureClass = operation.Dissolv(featureClass, comboxFiled.Text, textBox1.Text.Trim(), this.textBox2.Text.Trim() , this.progressBar1); IFeatureLayer pLayer = new FeatureLayerClass { FeatureClass = pFeatureClass, Name = pFeatureClass.AliasName }; this.m_axMapControl.AddLayer(pLayer); this.m_axMapControl.ActiveView.Refresh(); //释放资源 if (pFCursor != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(pFCursor); } if (pQueryFilter != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(pQueryFilter); } if (featureClass != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(featureClass); } MessageBox.Show(pLayer.Name + "处理完成!"); base.Close(); } }
public void Run(VectorOperation operation, MyMemoryBlock<float> a, MyMemoryBlock<float> b, MyMemoryBlock<float> result) { if (!Validate(operation, a.Count, b.Count)) return; switch (operation) { case VectorOperation.Rotate: { b.SafeCopyToHost(); float rads = DegreeToRadian(b.Host[0]); float[] transform = { (float)Math.Cos(rads), -(float)Math.Sin(rads), (float)Math.Sin(rads), (float)Math.Cos(rads) }; Array.Copy(transform, m_temp.Host, transform.Length); m_temp.SafeCopyToDevice(); m_matOperation.Run(MatOperation.Multiplication, m_temp, a, result); } break; case VectorOperation.Angle: { m_matOperation.Run(MatOperation.DotProd, a, b, result); result.SafeCopyToHost(); float dotProd = result.Host[0]; float angle = RadianToDegree((float)Math.Acos(dotProd)); result.Fill(0); result.Host[0] = angle; result.SafeCopyToDevice(); } break; case VectorOperation.DirectedAngle: { result.Host[0] = -90; result.SafeCopyToDevice(); Run(VectorOperation.Rotate, a, result, result); result.CopyToMemoryBlock(m_temp, 0, 0, result.Count); m_matOperation.Run(MatOperation.DotProd, a, b, result); result.SafeCopyToHost(); float dotProd = result.Host[0]; float angle; if (Math.Abs(Math.Abs(dotProd) - 1) < 1E-4) angle = 0; else angle = RadianToDegree((float)Math.Acos(dotProd)); m_matOperation.Run(MatOperation.DotProd, m_temp, b, result); result.SafeCopyToHost(); float perpDotProd = result.Host[0]; if (perpDotProd > 0) angle *= -1; result.Fill(0); result.Host[0] = angle; result.SafeCopyToDevice(); } break; } }
private bool Validate(VectorOperation operation, int sizeA, int sizeB) { if (operation == VectorOperation.None) return false; if ((operation & m_operations) == 0) { MyLog.WARNING.WriteLine("Trying to execute an uninitialized vector operation. Owner: " + m_caller.Name); return false; } if (operation != VectorOperation.Rotate && sizeA != sizeB) { MyLog.ERROR.WriteLine("Vectors have to be the same length for this operation. Owner: " + m_caller.Name); return false; } return true; }