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;
            }
        }
Beispiel #2
0
            public override void Execute()
            {
                int index = (int)CodeVector;

                if (Owner.Mode == CodeBookMode.GenerateOutput && Owner.InputAsOffset)
                {
                    Owner.Input.SafeCopyToHost();
                    int offset = (int)Owner.Input.Host[0];

                    if (index + offset < typeof(MyCodeVector).GetEnumValues().Length)
                    {
                        index += offset;
                    }
                }

                CudaDeviceVariable <float> codeVector = new CudaDeviceVariable <float>(
                    MyMemoryManager.Instance.GetGlobalVariable <float>(Owner.GlobalVariableName, Owner.GPU, Owner.GenerateRandomVectors).DevicePointer
                    + index * Owner.SymbolSize * sizeof(float), Owner.SymbolSize);

                if (Owner.Mode == CodeBookMode.GenerateOutput)
                {
                    if (lastIdx != index)
                    {
                        Owner.Output.GetDevice(Owner).CopyToDevice(codeVector, 0, 0, sizeof(float) * Owner.SymbolSize);
                    }
                }
                else
                {
                    if (Owner.UseBSCVariety)
                    {
                        m_similarityKernel.Run(codeVector.DevicePointer, Owner.Input, Owner.TempBlock, (int)SimilarityOperator, Owner.SymbolSize);
                        //ZXC m_sum.Run(Owner.Output, Owner.TempBlock, Owner.SymbolSize, 0, 0, 1, /* distributed: */ 0);
                        m_sum.size = Owner.SymbolSize;
                        m_sum.Run(Owner.Output, Owner.TempBlock);
                    }
                    else
                    {
                        //ZXC m_dot.Run(Owner.Output, 0, codeVector.DevicePointer, Owner.Input, Owner.SymbolSize, /* distributed: */ 0);
                        m_dot.Run(Owner.Output, codeVector.DevicePointer, Owner.Input.GetDevicePtr(Owner), Owner.SymbolSize);
                    }
                }

                lastIdx = index;
            }
            public float Mean(MyMemoryBlock <float> values, MyMemoryBlock <float> temporalMemory, int startIndex, int step, int stepsN)
            {
                float f = 0;

                if (stepsN >= HostToDeviceThreshold)
                {
                    //ZXC m_sumKernel.Run(temporalMemory, values, stepsN * step, 0, startIndex, step, /* distributed: */ 0);
                    m_sumKernel.size     = stepsN * step;
                    m_sumKernel.inOffset = startIndex;
                    m_sumKernel.stride   = step;
                    m_sumKernel.Run(temporalMemory, values);
                    Owner.Temp.SafeCopyToHost();
                    f = Owner.Temp.Host[0] / stepsN;
                    return(f);
                }

                for (int i = 0; i < stepsN; i++)
                {
                    int Idx = startIndex + i * step;
                    f += values.Host[startIndex + i * step];
                }
                f /= stepsN;
                return(f);
            }
Beispiel #4
0
            private void ExecuteK_Means()
            {
                //init all centroids into small circle around the center
                if (SimulationStep == 0)
                {
                    for (int i = 0; i < Owner.m_centroidsCount; i++)
                    {
                        double angle  = (double)i / Owner.m_centroidsCount * Math.PI * 2;
                        double radius = 0.2f;

                        double x = radius * Math.Cos(angle);
                        double y = radius * Math.Sin(angle);

                        Owner.Centroids.Host[CENTROID_FIELDS * i]     = (float)x;
                        Owner.Centroids.Host[CENTROID_FIELDS * i + 1] = (float)y;

                        Owner.Centroids.Host[CENTROID_FIELDS * i + 2] = 0;
                        Owner.Centroids.Host[CENTROID_FIELDS * i + 3] = 0;
                    }

                    Owner.Centroids.SafeCopyToDevice();
                    Owner.ControlValues.SafeCopyToDevice();
                }
                //process K-Means
                else
                {
                    //assign pixels to centroids
                    m_kernel.Run(Owner.AttentionMap, Owner.Centroids, Owner.m_centroidsCount, Owner.ReductionSources, m_imageWidth, m_imageHeight);

                    //sum centroid values for mean & stdDev
                    for (int i = 0; i < Owner.m_centroidsCount; i++)
                    {
                        for (int j = 0; j < Owner.m_sumsPerItem; j++)
                        {
                            //ZXC m_reduction_kernel.Run(Owner.Statistics, Owner.ReductionSources, Owner.AttentionMap.Count,
                            //    Owner.m_sumsPerItem * i + j, i * Owner.AttentionMap.Count * Owner.m_sumsPerItem + j * Owner.AttentionMap.Count, 1, /* distributed: */ 0);*/
                            m_reduction_kernel.outOffset = Owner.m_sumsPerItem * i + j;
                            m_reduction_kernel.inOffset  = i * Owner.AttentionMap.Count * Owner.m_sumsPerItem + j * Owner.AttentionMap.Count;
                            m_reduction_kernel.size      = Owner.AttentionMap.Count;
                            m_reduction_kernel.Run(Owner.Statistics, Owner.ReductionSources);
                        }
                    }

                    //sum all weights (denominator)
                    //ZXC m_reduction_kernel.Run(Owner.Statistics, Owner.AttentionMap, Owner.AttentionMap.Count, Owner.m_sumsPerItem * Owner.m_centroidsCount, 0, 1, /* distributed: */ 0);
                    m_reduction_kernel.outOffset = Owner.m_sumsPerItem * Owner.m_centroidsCount;
                    m_reduction_kernel.inOffset  = 0;
                    m_reduction_kernel.size      = Owner.AttentionMap.Count;
                    m_reduction_kernel.Run(Owner.Statistics, Owner.AttentionMap);

                    //evaluate new mean & stdDev for all centroids
                    m_finalize_kernel.SetupExecution(Owner.m_centroidsCount);
                    m_finalize_kernel.Run(Owner.Centroids, Owner.m_centroidsCount, Owner.Statistics, LearningRate, 0);

                    //TODO: move it to GPU
                    Owner.Centroids.SafeCopyToHost();
                    Owner.ControlValues.SafeCopyToHost();
                    Owner.Statistics.SafeCopyToHost();

                    FocusToCentroid();

                    if (SimulationStep % 2 == 0 && Owner.m_centroidsCount > 1)
                    {
                        DeleteUnusedCentroids();
                    }
                    else if (SimulationStep % 4 == 1 && Owner.m_centroidsCount < MAX_CENTROIDS * 0.125)
                    {
                        PerformSplit();
                    }
                    else if (Owner.m_centroidsCount > 1 && SimulationStep % 10 == 3)
                    {
                        PerformJoin();
                    }

                    int unusedCount = (MAX_CENTROIDS - Owner.m_centroidsCount) * CENTROID_FIELDS;
                    m_setKernel.SetupExecution(unusedCount);
                    m_setKernel.Run(Owner.Centroids, Owner.m_centroidsCount * CENTROID_FIELDS, float.PositiveInfinity, unusedCount);

                    Owner.Centroids.SafeCopyToHost();

                    Owner.PupilControl.SafeCopyToDevice();
                    Owner.CentroidImportance.SafeCopyToDevice();
                    Owner.ControlValues.SafeCopyToDevice();
                }
            }