コード例 #1
0
        public float RunReturn(DistanceOperation operation,
                               CudaDeviceVariable <float> A, int sizeA,
                               CudaDeviceVariable <float> B, int sizeB)
        {
            if (m_temp == null)
            {
                MyLog.ERROR.WriteLine("Init the object with a valid temp block of size at least one to enable RunReturn.");
                return(float.NaN);
            }

            switch (operation)
            {
            case DistanceOperation.None:
                return(float.NaN);

            case DistanceOperation.EuclidDist:
                Run(DistanceOperation.EuclidDistSquared, A, sizeA, B, sizeB, m_temp.GetDevice(m_caller), sizeA);
                m_temp.SafeCopyToHost(0, 1);

                return((float)Math.Sqrt(m_temp.Host[0]));

            default:
                Run(operation, A, sizeA, B, sizeB, m_temp.GetDevice(m_caller), sizeA);
                m_temp.SafeCopyToHost(0, 1);

                return(m_temp.Host[0]);
            }
        }
コード例 #2
0
        public void Run(DistanceOperation operation,
                        CudaDeviceVariable <float> A, int sizeA,
                        CudaDeviceVariable <float> B, int sizeB,
                        CudaDeviceVariable <float> result, int sizeRes)
        {
            if (!ValidateAtRun(operation))
            {
                return;
            }

            switch (operation)
            {
            case DistanceOperation.DotProd:
                //ZXC m_dotKernel.Run(result.DevicePointer, 0, A.DevicePointer, B.DevicePointer, sizeA, 0);
                m_dotKernel.Run(result.DevicePointer, A.DevicePointer, B.DevicePointer, sizeA);
                break;

            case DistanceOperation.CosDist:
                //ZXC m_cosKernel.Run(result.DevicePointer, 0, A.DevicePointer, B.DevicePointer, sizeA, 0);
                m_cosKernel.Run(result.DevicePointer, A.DevicePointer, B.DevicePointer, sizeA);
                break;

            case DistanceOperation.EuclidDist:
                float res = RunReturn(operation, A, sizeA, B, sizeB);
                result.CopyToDevice(res);
                break;

            case DistanceOperation.EuclidDistSquared:
                m_combineVecsKernel.SetupExecution(sizeA);
                m_combineVecsKernel.Run(A.DevicePointer, B.DevicePointer, m_temp, (int)MyJoin.MyJoinOperation.Subtraction, sizeA);
                //ZXC m_dotKernel.Run(result.DevicePointer, 0, m_temp, m_temp, m_temp.Count, 0);
                m_dotKernel.Run(result.DevicePointer, m_temp, m_temp);
                break;

            case DistanceOperation.HammingDist:
                m_combineVecsKernel.SetupExecution(sizeA);
                m_combineVecsKernel.Run(A.DevicePointer, B.DevicePointer, m_temp, (int)MyJoin.MyJoinOperation.Equal, sizeA);
                //ZXC m_reduceSumKernel.Run(result.DevicePointer, m_temp, m_temp.Count, 0, 0, 1, /*distributed = false*/0); // reduction to a single number
                m_reduceSumKernel.Run(result.DevicePointer, m_temp);
                float fDist = 0;     // to transform number of matches to a number of differences
                result.CopyToHost(ref fDist);
                fDist = m_temp.Count - fDist;
                result.CopyToDevice(fDist);
                break;

            case DistanceOperation.HammingSim:
                m_combineVecsKernel.SetupExecution(sizeA);
                m_combineVecsKernel.Run(A.DevicePointer, B.DevicePointer, m_temp, (int)MyJoin.MyJoinOperation.Equal, sizeA);
                //ZXC m_reduceSumKernel.Run(result.DevicePointer, m_temp, m_temp.Count, 0, 0, 1, /*distributed = false*/0); // reduction to a single number
                m_reduceSumKernel.Run(result.DevicePointer, m_temp);
                // take the single number (number of different bits) and convert it to Hamming Similarity:
                // a number in range <0,1> that says how much the vectors are similar
                float fSim = 0;
                result.CopyToHost(ref fSim);
                fSim = fSim / m_temp.Count;
                result.CopyToDevice(fSim);
                break;
            }
        }
コード例 #3
0
        private DistanceOperation insertDistanceOperation(DistanceOperation type, TreeNode parent)
        {
            DistanceOperation n = new DistanceOperation(type);

            if (parent == null)
            {
                tree.Nodes.Add(n.TreeNode);
            }
            else
            {
                parent.Nodes.Add(n.TreeNode);
            }
            return(n);
        }
コード例 #4
0
        // can be called during simulation
        public bool ValidateAtRun(DistanceOperation operation)
        {
            if (operation == DistanceOperation.None)
            {
                return(false);
            }

            if ((operation & m_operations) == 0)
            {
                MyLog.WARNING.WriteLine("Trying to execute an uninitialized distance operation. Owner: " + m_caller.Name);
                return(false);
            }

            return(true);
        }
コード例 #5
0
        public bool Load(string nodesXMLFilename)
        {
            try
            {
                XElement x = XElement.Load(nodesXMLFilename, LoadOptions.None);

                XElement settings = x.Element("settings");
                foreach (XElement n in x.Elements("node"))
                {
                    switch (n.Attribute("type").Value)
                    {
                    case "do":
                        DistancePrimitive dp = new DistancePrimitive(n.Attribute("name").Value, n.Attribute("caption").Value, n.Attribute("code").Value);
                        foreach (XElement p in n.Elements("prop"))
                        {
                            dp.Properties.Add(createInputProperty(p.Attribute("type").Value, p.Attribute("name").Value, p.Attribute("default").Value));
                        }
                        DistanceFieldTypes.Add(dp);
                        break;

                    case "distop":
                        DistanceOperation distop = new DistanceOperation(n.Attribute("name").Value, n.Attribute("caption").Value, n.Attribute("code").Value);
                        foreach (XElement p in n.Elements("prop"))
                        {
                            distop.Properties.Add(createInputProperty(p.Attribute("type").Value, p.Attribute("name").Value, p.Attribute("default").Value));
                        }
                        DistanceOperations.Add(distop);
                        break;

                    case "domop":
                        DomainOperation dop = new DomainOperation(n.Attribute("name").Value, n.Attribute("caption").Value, n.Attribute("code").Value);
                        foreach (XElement p in n.Elements("prop"))
                        {
                            dop.Properties.Add(createInputProperty(p.Attribute("type").Value, p.Attribute("name").Value, p.Attribute("default").Value));
                        }
                        DomainOperations.Add(dop);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                errors.Add("Failed to load nodes.xml (" + e.Message + ")");
                return(false);
            }

            return(true);
        }
コード例 #6
0
        public MyDistanceOps(MyWorkingNode caller, DistanceOperation operations, MyMemoryBlock <float> tempBlock = null)
        {
            m_caller     = caller;
            m_operations = operations;
            m_temp       = tempBlock;


            if (operations.HasFlag(DistanceOperation.DotProd))
            {
                m_dotKernel = MyKernelFactory.Instance.KernelProduct <float>(caller, caller.GPU, ProductMode.f_DotProduct_f);
            }

            if (operations.HasFlag(DistanceOperation.CosDist))
            {
                m_cosKernel = MyKernelFactory.Instance.KernelProduct <float>(caller, caller.GPU, ProductMode.f_Cosine_f);
            }

            if (operations.HasFlag(DistanceOperation.EuclidDist) || operations.HasFlag(DistanceOperation.EuclidDistSquared))
            {
                // EuclidDist computes EuclidDistSquared first, so keep them together:
                m_operations |= DistanceOperation.EuclidDist | DistanceOperation.EuclidDistSquared;
                m_dotKernel   = MyKernelFactory.Instance.KernelProduct <float>(caller, caller.GPU, ProductMode.f_DotProduct_f);
            }

            if (operations.HasFlag(DistanceOperation.HammingDist))
            {
                m_reduceSumKernel = MyKernelFactory.Instance.KernelReduction <float>(caller, caller.GPU, ReductionMode.f_Sum_f);
            }
            if (operations.HasFlag(DistanceOperation.HammingSim))
            {
                m_reduceSumKernel = MyKernelFactory.Instance.KernelReduction <float>(caller, caller.GPU, ReductionMode.f_Sum_f);
            }

            if (operations.HasFlag(DistanceOperation.EuclidDist) || operations.HasFlag(DistanceOperation.EuclidDistSquared) ||
                operations.HasFlag(DistanceOperation.HammingDist) || operations.HasFlag(DistanceOperation.HammingSim))
            {
                m_combineVecsKernel = MyKernelFactory.Instance.Kernel(m_caller.GPU, @"Common\CombineVectorsKernel", "CombineTwoVectorsKernel");
            }
        }
コード例 #7
0
        public MyDistanceOps(MyWorkingNode caller, DistanceOperation operations, MyMemoryBlock<float> tempBlock = null)
        {
            m_caller = caller;
            m_operations = operations;
            m_temp = tempBlock;

            if (operations.HasFlag(DistanceOperation.DotProd))
            {
                m_dotKernel = MyKernelFactory.Instance.KernelProduct<float>(caller, caller.GPU, ProductMode.f_DotProduct_f);
            }

            if (operations.HasFlag(DistanceOperation.CosDist))
            {
                m_cosKernel = MyKernelFactory.Instance.KernelProduct<float>(caller, caller.GPU, ProductMode.f_Cosine_f);
            }

            if (operations.HasFlag(DistanceOperation.EuclidDist) || operations.HasFlag(DistanceOperation.EuclidDistSquared))
            {
                // EuclidDist computes EuclidDistSquared first, so keep them together:
                m_operations |= DistanceOperation.EuclidDist | DistanceOperation.EuclidDistSquared;
                m_dotKernel = MyKernelFactory.Instance.KernelProduct<float>(caller, caller.GPU, ProductMode.f_DotProduct_f);
            }

            if (operations.HasFlag(DistanceOperation.HammingDist))
            {
                m_reduceSumKernel = MyKernelFactory.Instance.KernelReduction<float>(caller, caller.GPU, ReductionMode.f_Sum_f);
            }
            if (operations.HasFlag(DistanceOperation.HammingSim))
            {
                m_reduceSumKernel = MyKernelFactory.Instance.KernelReduction<float>(caller, caller.GPU, ReductionMode.f_Sum_f);
            }

            if (operations.HasFlag(DistanceOperation.EuclidDist) || operations.HasFlag(DistanceOperation.EuclidDistSquared) ||
                operations.HasFlag(DistanceOperation.HammingDist) || operations.HasFlag(DistanceOperation.HammingSim))
            {
                m_combineVecsKernel = MyKernelFactory.Instance.Kernel(m_caller.GPU, @"Common\CombineVectorsKernel", "CombineTwoVectorsKernel");
            }
        }
コード例 #8
0
        // should be called before simulation is started
        public static bool Validate(DistanceOperation operation, int sizeA, int sizeB, int sizeTemp, int sizeRes, out string errorOutput)
        {
            errorOutput = null;

            switch (operation)
            {
            case DistanceOperation.None:
                return(true);

            case DistanceOperation.DotProd:
            case DistanceOperation.CosDist:
                break;

            case DistanceOperation.EuclidDist:
            case DistanceOperation.EuclidDistSquared:
            case DistanceOperation.HammingDist:
            case DistanceOperation.HammingSim:
                if (sizeA != sizeTemp)
                {
                    errorOutput = "Invalid temp block size for the distance operation.";
                    return(false);
                }
                break;

            default:
                errorOutput = "Invalid operation. Only a single value within the enum range should be passed.";
                return(false);
            }

            if (sizeA != sizeB || sizeRes != 1)
            {
                errorOutput = "Invalid input/output block sizes for the distance operation.";
                return(false);
            }

            return(true);
        }
コード例 #9
0
 public void Run(DistanceOperation operation, MyMemoryBlock <float> A, MyMemoryBlock <float> B, MyMemoryBlock <float> result)
 {
     Run(operation, A.GetDevice(m_caller), A.Count, B.GetDevice(m_caller), B.Count, result.GetDevice(m_caller), result.Count);
 }
コード例 #10
0
        // can be called during simulation
        public bool ValidateAtRun(DistanceOperation operation)
        {
            if (operation == DistanceOperation.None)
                return false;

            if ((operation & m_operations) == 0)
            {
                MyLog.WARNING.WriteLine("Trying to execute an uninitialized distance operation. Owner: " + m_caller.Name);
                return false;
            }

            return true;
        }
コード例 #11
0
 public float RunReturn(DistanceOperation operation, MyMemoryBlock<float> A, MyMemoryBlock<float> B)
 {
     return RunReturn(operation, A.GetDevice(m_caller), A.Count, B.GetDevice(m_caller), B.Count);
 }
コード例 #12
0
        public float RunReturn(DistanceOperation operation,
            CudaDeviceVariable<float> A, int sizeA,
            CudaDeviceVariable<float> B, int sizeB)
        {
            if (m_temp == null)
            {
                MyLog.ERROR.WriteLine("Init the object with a valid temp block of size at least one to enable RunReturn.");
                return float.NaN;
            }

            switch (operation)
            {
                case DistanceOperation.None:
                    return float.NaN;

                case DistanceOperation.EuclidDist:
                    Run(DistanceOperation.EuclidDistSquared, A, sizeA, B, sizeB, m_temp.GetDevice(m_caller), sizeA);
                    m_temp.SafeCopyToHost(0, 1);

                    return (float)Math.Sqrt(m_temp.Host[0]);

                default:
                    Run(operation, A, sizeA, B, sizeB, m_temp.GetDevice(m_caller), sizeA);
                    m_temp.SafeCopyToHost(0, 1);

                    return m_temp.Host[0];
            }
        }
コード例 #13
0
 public void Run(DistanceOperation operation, MyMemoryBlock<float> A, MyMemoryBlock<float> B, MyMemoryBlock<float> result)
 {
     Run(operation, A.GetDevice(m_caller), A.Count, B.GetDevice(m_caller), B.Count, result.GetDevice(m_caller), result.Count);
 }
コード例 #14
0
        public void Run(DistanceOperation operation,
            CudaDeviceVariable<float> A, int sizeA,
            CudaDeviceVariable<float> B, int sizeB,
            CudaDeviceVariable<float> result, int sizeRes)
        {
            if (!ValidateAtRun(operation))
                return;

            switch (operation)
            {
                case DistanceOperation.DotProd:
                    //ZXC m_dotKernel.Run(result.DevicePointer, 0, A.DevicePointer, B.DevicePointer, sizeA, 0);
                    m_dotKernel.Run(result.DevicePointer, A.DevicePointer, B.DevicePointer, sizeA);
                    break;

                case DistanceOperation.CosDist:
                    //ZXC m_cosKernel.Run(result.DevicePointer, 0, A.DevicePointer, B.DevicePointer, sizeA, 0);
                    m_cosKernel.Run(result.DevicePointer, A.DevicePointer, B.DevicePointer, sizeA);
                    break;

                case DistanceOperation.EuclidDist:
                    float res = RunReturn(operation, A, sizeA, B, sizeB);
                    result.CopyToDevice(res);
                    break;

                case DistanceOperation.EuclidDistSquared:
                    m_combineVecsKernel.SetupExecution(sizeA);
                    m_combineVecsKernel.Run(A.DevicePointer, B.DevicePointer, m_temp, (int)MyJoin.MyJoinOperation.Subtraction, sizeA);
                    //ZXC m_dotKernel.Run(result.DevicePointer, 0, m_temp, m_temp, m_temp.Count, 0);
                    m_dotKernel.Run(result.DevicePointer, m_temp, m_temp);
                    break;

                case DistanceOperation.HammingDist:
                    m_combineVecsKernel.SetupExecution(sizeA);
                    m_combineVecsKernel.Run(A.DevicePointer, B.DevicePointer, m_temp, (int)MyJoin.MyJoinOperation.Equal, sizeA);
                    //ZXC m_reduceSumKernel.Run(result.DevicePointer, m_temp, m_temp.Count, 0, 0, 1, /*distributed = false*/0); // reduction to a single number
                    m_reduceSumKernel.Run(result.DevicePointer, m_temp);
                    float fDist = 0; // to transform number of matches to a number of differences
                    result.CopyToHost(ref fDist);
                    fDist = m_temp.Count - fDist;
                    result.CopyToDevice(fDist);
                    break;

                case DistanceOperation.HammingSim:
                    m_combineVecsKernel.SetupExecution(sizeA);
                    m_combineVecsKernel.Run(A.DevicePointer, B.DevicePointer, m_temp, (int)MyJoin.MyJoinOperation.Equal, sizeA);
                    //ZXC m_reduceSumKernel.Run(result.DevicePointer, m_temp, m_temp.Count, 0, 0, 1, /*distributed = false*/0); // reduction to a single number
                    m_reduceSumKernel.Run(result.DevicePointer, m_temp);
                    // take the single number (number of different bits) and convert it to Hamming Similarity:
                    // a number in range <0,1> that says how much the vectors are similar
                    float fSim = 0;
                    result.CopyToHost(ref fSim);
                    fSim = fSim / m_temp.Count;
                    result.CopyToDevice(fSim);
                    break;
            }
        }
コード例 #15
0
        // should be called before simulation is started
        public static bool Validate(DistanceOperation operation, int sizeA, int sizeB, int sizeTemp, int sizeRes, out string errorOutput)
        {
            errorOutput = null;

            switch (operation)
            {
                case DistanceOperation.None:
                    return true;

                case DistanceOperation.DotProd:
                case DistanceOperation.CosDist:
                    break;

                case DistanceOperation.EuclidDist:
                case DistanceOperation.EuclidDistSquared:
                case DistanceOperation.HammingDist:
                case DistanceOperation.HammingSim:
                    if (sizeA != sizeTemp)
                    {
                        errorOutput = "Invalid temp block size for the distance operation.";
                        return false;
                    }
                    break;

                default:
                    errorOutput = "Invalid operation. Only a single value within the enum range should be passed.";
                    return false;
            }

            if (sizeA != sizeB || sizeRes != 1)
            {
                errorOutput = "Invalid input/output block sizes for the distance operation.";
                return false;
            }

            return true;
        }
コード例 #16
0
        public bool Setup(string functionsXMLFilename)
        {
            try
            {
                XElement x = XElement.Load(functionsXMLFilename, LoadOptions.None);

                XElement helpers = x.Element("helpers");
                foreach (XElement h in helpers.Elements("code"))
                {
                    XAttribute comment = h.Attribute("comment");
                    HelperCode helper  = new HelperCode(h.Attribute("name").Value, h.Value, comment == null ? "" : comment.Value);
                    Helpers.Add(helper.Name, helper);
                }

                XElement nodes = x.Element("nodes");

                foreach (XElement n in nodes.Elements("node"))
                {
                    string   code     = "";
                    string[] requires = null;

                    XElement func = n.Element("function");
                    if (func != null)
                    {
                        code = func.Value;
                        XAttribute requ = func.Attribute("requires");
                        if (requ != null)
                        {
                            requires = requ.Value.Split(",".ToCharArray());
                        }
                    }

                    XAttribute com     = n.Attribute("comment");
                    string     comment = "";
                    if (com != null)
                    {
                        comment = com.Value;
                    }

                    SceneNode node = null;

                    switch (n.Attribute("type").Value)
                    {
                    case "dp":
                        node = new DistancePrimitive(n.Attribute("name").Value, n.Attribute("caption").Value, n.Attribute("mask").Value, code, requires, comment);
                        DistanceFieldTypes.Add(node as DistancePrimitive);
                        break;

                    case "distop":
                        node = new DistanceOperation(n.Attribute("name").Value, n.Attribute("caption").Value, n.Attribute("mask").Value, code, requires, comment);
                        DistanceOperations.Add(node as DistanceOperation);
                        break;

                    case "domop":
                        node = new DomainOperation(n.Attribute("name").Value, n.Attribute("caption").Value, n.Attribute("mask").Value, code, requires, comment);
                        DomainOperations.Add(node as DomainOperation);
                        break;
                    }

                    if (node != null && n.Element("properties") != null)
                    {
                        foreach (XElement p in n.Element("properties").Elements("property"))
                        {
                            node.Properties.Add(createInputProperty(p.Attribute("type").Value, p.Attribute("name").Value, p.Attribute("default").Value));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                errors.Add("Failed to load/parse " + functionsXMLFilename + ":\n" + e.Message);
                return(false);
            }

            return(true);
        }
コード例 #17
0
 public float RunReturn(DistanceOperation operation, MyMemoryBlock <float> A, MyMemoryBlock <float> B)
 {
     return(RunReturn(operation, A.GetDevice(m_caller), A.Count, B.GetDevice(m_caller), B.Count));
 }