Пример #1
0
		public CGNormalCellArea(ArrayView array) {
			if (array.sheet.IsFunctionSheet) {
				this.index = -1; // Illegal cell area
			}
			else {
				this.index = valueTable.GetIndex(array);
			}
		}
Пример #2
0
        public void train(int topN)
        {
            _topN = topN;
            _randomRecommends = ArrayView<string>.Create(primeNumber * topN);
            var isbns = trainData.Select(e => e.ISBN).Distinct().ToArray();

            Parallel.ForEach(Enumerable.Range(0, primeNumber), new ParallelOptions { MaxDegreeOfParallelism = 4 }, seed =>
                {
                    var rand = new Random(seed);
                    var visited = new HashSet<int>();
                    var view = _randomRecommends.SubArray(seed* topN, _topN);
                    var viewIndex = 0;
                    while (viewIndex < topN)
                    {
                        var isbnIndex = rand.Next(isbns.Length);
                        if (!visited.Add(isbnIndex))
                            continue;
                        view[viewIndex++] = isbns[isbnIndex];
                    }
                });
        }
Пример #3
0
 /// <summary>
 ///A simple 1D kernel. By default, all kernels can access static readonly
 /// fields (since they are immutable after their initialization).
 /// <param name="index">The current thread index.</param>
 /// <param name="dataView">The view pointing to our memory buffer.</param>
 static void StaticFieldAccessKernel(
     Index1 index,
     ArrayView <int> dataView)
 {
     dataView[index] = ReadOnlyValue;
 }
Пример #4
0
 private static void ApplyKernel(
     Index index, /* The global thread index (1D in this case) */
     ArrayView <Rgba32> pixelArray /* A view to a chunk of memory (1D in this case)*/)
 {
     pixelArray[index] = Invert(pixelArray[index]);
 }
Пример #5
0
        internal static void ArrayViewValidKernel(Index1 index, ArrayView <int> data)
        {
            ArrayView <int> invalid = default;

            data[index] = (data.IsValid ? 1 : 0) + (!invalid.IsValid ? 1 : 0);
        }
Пример #6
0
        private static void strategyInvalidateCluster(Index1 index, ArrayView <int> bobbuff, ArrayView <int> bobbuff2, ArrayView2D <int> newPals, ArrayView2D <int> clusbuff)
        {
            int i1 = bobbuff[index];
            int i2 = bobbuff2[index];

            int i;

            for (i = 0; i < clusbuff.Extent.Y; i++)
            {
                if (clusbuff[i1, i] == 0)
                {
                    break;
                }
                else
                {
                    newPals[index, i] = clusbuff[i1, i];
                }
            }

            int found;

            for (int j = 0; j < clusbuff.Extent.Y; j++)
            {
                found = 0;
                if (clusbuff[i2, j] != 0)
                {
                    for (int k = 0; k < clusbuff.Extent.Y; k++)
                    {
                        if (newPals[index, k] == clusbuff[i2, j])
                        {
                            found = 1;
                            break;
                        }
                    }
                }
                if (found == 0)
                {
                    newPals[index, i] = clusbuff[i2, j];
                    i++;
                }
            }
        }
Пример #7
0
 /// <summary>
 /// Example of using Interop.WriteLine within a kernel to display output.
 /// </summary>
 static void WriteLineKernel(Index1D index, ArrayView <int> dataView)
 {
     // NB: String interpolation, alignment, spacing, format and precision
     // specifiers are not currently supported. Use standard {x} placeholders.
     Interop.WriteLine("{0} = {1}", index, dataView[index]);
 }
Пример #8
0
        public static void GenerateKernel(Index1 index, int width, float scaleXY, ArrayView <float> dataOut, ArrayView <byte> rand, float mult)
        {
            float x = ((index) % width) * scaleXY;
            float y = ((index) / width) * scaleXY;

            const float F2 = 0.366025403f; // F2 = 0.5*(sqrt(3.0)-1.0)
            const float G2 = 0.211324865f; // G2 = (3.0-Math.sqrt(3.0))/6.0

            float n0, n1, n2;              // Noise contributions from the three corners

            // Skew the input space to determine which simplex cell we're in
            float s  = (x + y) * F2; // Hairy factor for 2D
            float xs = x + s;
            float ys = y + s;
            int   i  = FastFloor(xs);
            int   j  = FastFloor(ys);

            float t  = (float)(i + j) * G2;
            float X0 = i - t;  // Unskew the cell origin back to (x,y) space
            float Y0 = j - t;
            float x0 = x - X0; // The x,y distances from the cell origin
            float y0 = y - Y0;

            // For the 2D case, the simplex shape is an equilateral triangle.
            // Determine which simplex we are in.
            int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords

            if (x0 > y0)
            {
                i1 = 1; j1 = 0;
            }                                // lower triangle, XY order: (0,0)->(1,0)->(1,1)
            else
            {
                i1 = 0; j1 = 1;
            }                             // upper triangle, YX order: (0,0)->(0,1)->(1,1)

            // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
            // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
            // c = (3-sqrt(3))/6

            float x1 = x0 - i1 + G2;          // Offsets for middle corner in (x,y) unskewed coords
            float y1 = y0 - j1 + G2;
            float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
            float y2 = y0 - 1.0f + 2.0f * G2;

            // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
            int ii = Mod(i, 256);
            int jj = Mod(j, 256);

            // Calculate the contribution from the three corners
            float t0 = 0.5f - x0 * x0 - y0 * y0;

            if (t0 < 0.0f)
            {
                n0 = 0.0f;
            }
            else
            {
                t0 *= t0;
                n0  = t0 * t0 * grad(rand[ii + rand[jj]], x0, y0);
            }

            float t1 = 0.5f - x1 * x1 - y1 * y1;

            if (t1 < 0.0f)
            {
                n1 = 0.0f;
            }
            else
            {
                t1 *= t1;
                n1  = t1 * t1 * grad(rand[ii + i1 + rand[jj + j1]], x1, y1);
            }

            float t2 = 0.5f - x2 * x2 - y2 * y2;

            if (t2 < 0.0f)
            {
                n2 = 0.0f;
            }
            else
            {
                t2 *= t2;
                n2  = t2 * t2 * grad(rand[ii + 1 + rand[jj + 1]], x2, y2);
            }

            // Add contributions from each corner to get the final noise value.
            // The result is scaled to return values in the interval [-1,1].
            float val = 40.0f * (n0 + n1 + n2);

            dataOut[index] = (val * mult);
        }
Пример #9
0
        public static void CalculateBrightnessStats(Index index, ArrayView3D <short> input, ArrayView <double> meanBrightness, ArrayView <short> maxBrightness, ArrayView <double> standartDeviation)
        {
            long sum = 0;

            short max = 0;

            var band = input.GetSliceView(index).AsLinearView();

            for (int i = 0; i < band.Length; i++)
            {
                var val = band[i];
                if (val < 0)
                {
                    val = 0;
                }
                //for deviation and mean sum
                sum += val;
                if (val > max)
                {
                    max = val;
                }
            }

            double mean = sum / (double)band.Length;

            meanBrightness[index] = mean;
            maxBrightness[index]  = max;

            double dividend = 0;

            for (int i = 0; i < band.Length; i++)
            {
                var val = band[i];
                if (val < 0)
                {
                    val = 0;
                }

                dividend += XMath.Pow(XMath.Abs(val - mean), 2);
            }

            standartDeviation[index] = XMath.Sqrt(dividend / band.Length);
        }
        public static void _V_X_M_line_M(Index2 size, ArrayView2D <float> output, ArrayView2D <float> m, ArrayView <float> v)
        {
            int x = size.X;
            int y = size.Y;

            output[x, y] = v[x] * m[x, y];
        }
        public static void _V_sub_V(Index size, ArrayView <float> output, ArrayView <float> v0, ArrayView <float> v1)
        {
            int x = size.X;

            output[x] = v0[x] - v1[x];
        }
        public static void _V_X_V_M(Index2 size, ArrayView2D <float> output, ArrayView <float> v0, ArrayView <float> v1)
        {
            int x = size.X;
            int y = size.Y;

            output[x, y] = v0[x] * v1[y];
        }
        public static void _M_2_columns_V(Index size, ArrayView <float> v, ArrayView2D <float> m)
        {
            int y = size.X;

            v[y] = m[0, y] + m[1, y];
        }
        public static void _M_2_lines_V(Index size, ArrayView <float> v, ArrayView2D <float> m)
        {
            int x = size.X;

            v[x] = m[x, 0] + m[x, 1];
        }
        public static void _V_Max(Index size, ArrayView <float> output, ArrayView <float> v, ArrayView <float> v1)
        {
            int x = size.X;

            output[x] = GPUMath.Max(v[x], v1[x]);
        }
        public static void _V_C_More_Equal(Index size, ArrayView <float> output, float c, ArrayView <float> v)
        {
            int x = size.X;

            output[x] = v[x] >= c ? 1 : 0;
        }
        public static void _V_Exp(Index size, ArrayView <float> output, ArrayView <float> v)
        {
            int x = size.X;

            output[x] = GPUMath.Exp(v[x]);
        }
        public static void _C_X_V(Index size, float c, ArrayView <float> output, ArrayView <float> v)
        {
            int x = size.X;

            output[x] = c * v[x];
        }
Пример #19
0
 /// <summary>
 /// A simple 1D kernel that references types which in turn reference internal
 /// custom types of the current assembly.
 /// </summary>
 /// <param name="index">The current thread index.</param>
 /// <param name="dataView">The view pointing to our memory buffer.</param>
 static void MyKernel(
     Index index,
     ArrayView <CustomDataType> dataView)
 {
     dataView[index] = new CustomDataType(index);
 }
Пример #20
0
 internal static void ArrayViewLengthInBytesKernel(
     Index index,
     ArrayView <int> data)
 {
     data[index] = data.LengthInBytes;
 }
Пример #21
0
 internal static void SharedMemoryVariableKernel(ArrayView <int> data)
 {
     ref var sharedMemory = ref ILGPU.SharedMemory.Allocate <int>();
Пример #22
0
        public static void DistanceKernel(Index1 index, ArrayView <int> gen, ArrayView2D <int> cities, ArrayView <double> result)
        {
            double sum = 0;

            if (index == 51)
            {
                sum += Distance.Euclidean(new double[] { cities[gen[index], 0], cities[gen[index], 1] }, new double[] { cities[gen[0], 0], cities[gen[0], 1] });
            }
            else
            {
                sum += Distance.Euclidean(new double[] { cities[gen[index], 0], cities[gen[index], 1] }, new double[] { cities[gen[index + 1], 0], cities[gen[index + 1], 1] });
            }


            result[index] = sum;
        }
Пример #23
0
 /// <summary>
 /// Constructs a new debug view.
 /// </summary>
 /// <param name="source">The source array view.</param>
 protected BaseDebugArrayView(ArrayView <T> source)
 {
     view = source;
 }
Пример #24
0
 // KERNEL
 static void AccessSliceKernal(Index1 index, ArrayView <float> OutPut, ArrayView <float> Input, ArrayView <int> ChangeSelectLength)
 {
     OutPut[index] = Input[
         index * ChangeSelectLength[0] * ChangeSelectLength[4] + // iRcL
         index * ChangeSelectLength[1] +                         // iCc
         ChangeSelectLength[2] * ChangeSelectLength[4] +         // RsL
         ChangeSelectLength[3]];                                 // Cs
 }
Пример #25
0
 internal static void ArrayViewExtentKernel(
     Index1 index,
     ArrayView <int> data)
 {
     data[index] = data.Extent.X;
 }
Пример #26
0
 internal static void Index1EntryPointKernel(Index1 index, ArrayView <int> output)
 {
     output[index] = index;
 }
Пример #27
0
 internal static void ArrayViewLengthKernel(
     Index1 index,
     ArrayView <int> data)
 {
     data[index] = data.Length;
 }
Пример #28
0
 public void InstanceKernel(Index1 index, ArrayView <int> output)
 {
     output[index] = NestedFunction(index);
 }
Пример #29
0
 /// <summary>
 /// A simple 1D kernel. By default, all kernels can access global constants
 /// (since they will be inlined by the compiler by default).
 /// <param name="index">The current thread index.</param>
 /// <param name="dataView">The view pointing to our memory buffer.</param>
 static void ConstantKernel(
     Index1 index,
     ArrayView <int> dataView)
 {
     dataView[index] = ConstantValue;
 }
Пример #30
0
            public void InstanceKernel(Index3 index, ArrayView <int> output, Index3 extent)
            {
                var linearIndex = index.ComputeLinearIndex(extent);

                output[linearIndex] = NestedFunction(linearIndex);
            }
Пример #31
0
 internal static void NestedCallKernel(
     Index1 index,
     ArrayView <int> data)
 {
     data[index] = GetValue();
 }
Пример #32
0
        public static void CalculateBrightnessMap(Index index, ArrayView <double> meanBrightness, ArrayView <short> maxBrightness, ArrayView <double> standartDeviation, ArrayView2D <int> image, int meanBrColor, int maxBrColor, int devBrColor, double heightScale)
        {
            int xCoord1 = (int)((index / (float)maxBrightness.Extent.Size) * image.Width);
            int xCoord2 = (int)(((index + 1) / (float)maxBrightness.Extent.Size) * image.Width - 1);

            for (int i = xCoord1; i < xCoord2; i++)
            {
                image[i, (int)(image.Height - meanBrightness[index] * heightScale)]    = meanBrColor;
                image[i, (int)(image.Height - maxBrightness[index] * heightScale)]     = maxBrColor;
                image[i, (int)(image.Height - standartDeviation[index] * heightScale)] = devBrColor;
            }
        }
Пример #33
0
        public void train(int topN)
        {
            _topN = topN;

            BestSellerRecommendList = ArrayView<string>.Of(trainData
                .GroupBy(e => e.ISBN)
                .Select(e => new { ISBN = e.Key, Count = e.Count() })
                .OrderByDescending(e => e.Count)
                .Select(e => e.ISBN)
                .Take(topN));

            isbnSellCountList = trainData
                .GroupBy(e => e.ISBN)
                .Select(e => new { ISBN = e.Key, Count = e.Count() })
                .OrderByDescending(e => e.Count)
                .Select(e => Tuple.Create(e.ISBN, e.Count))
                .ToList();

            userISBNDic = trainData
                .GroupBy(e => e.회원번호)
                .Select(e => new { UserId = e.Key, ISBNSet = new HashSet<string>(e.Select(t => t.ISBN)) })
                .ToDictionary(e => e.UserId, e => e.ISBNSet);

            countOfOneItemDic = trainData
                .GroupBy(e => e.ISBN)
                .Select(e => new { ISBN = e.Key, Count = e.Count() })
                .ToDictionary(e => e.ISBN, e => e.Count);

            Parallel.ForEach(userISBNDic, new ParallelOptions { MaxDegreeOfParallelism = 3 }, e =>
            {
                var userISBNList = e.Value.ToList();
                for (int i = 0; i < userISBNList.Count; i++)
                {
                    for (int j = i + 1; j < userISBNList.Count; j++)
                    {
                        var item1 = userISBNList[i];
                        var item2 = userISBNList[j];
                        var itemKey = makeKey(item1, item2);

                        lock(countOfTwoItemsDic)
                        {
                            if (countOfTwoItemsDic.ContainsKey(itemKey))
                                countOfTwoItemsDic[itemKey] += 1;
                            else
                                countOfTwoItemsDic.Add(itemKey, 1);
                        }
                    }
                }
            });

            var oneItemCountWriter = new StreamWriter("count_of_one_item.tsv");
            foreach (var elem in countOfOneItemDic)
            {
                oneItemCountWriter.Write(elem.Key.Trim());
                oneItemCountWriter.Write("\t");
                oneItemCountWriter.Write(elem.Value);
                oneItemCountWriter.WriteLine();
            }
            oneItemCountWriter.Close();

            var twoItemsCountWriter = new StreamWriter("count_of_two_items.tsv");
            foreach (var elem in countOfTwoItemsDic)
            {
                twoItemsCountWriter.Write(elem.Key.Item1.Trim());
                twoItemsCountWriter.Write("\t");
                twoItemsCountWriter.Write(elem.Key.Item2.Trim());
                twoItemsCountWriter.Write("\t");
                twoItemsCountWriter.Write(elem.Value);
                twoItemsCountWriter.WriteLine();
            }
            twoItemsCountWriter.Close();

            double sumOfOne = countOfOneItemDic.Sum(e => e.Value);
            double sumOfTwo = countOfTwoItemsDic.Sum(e => e.Value);

            foreach (var elem in countOfTwoItemsDic.Keys)
            {
                if (countOfOneItemDic[elem.Item1] < 10) continue;
                if (countOfOneItemDic[elem.Item2] < 10) continue;

                var pOfItem1 = countOfOneItemDic[elem.Item1] / sumOfOne;
                var pOfItem2 = countOfOneItemDic[elem.Item2] / sumOfOne;
                var pOfTwoItems = countOfTwoItemsDic[elem] / sumOfTwo;
                var pmi = Math.Log(pOfTwoItems) - Math.Log(pOfItem1) - Math.Log(pOfItem2);
                var npmi = pmi / (-Math.Log(pOfTwoItems));

                nPMIDic.Add(elem, npmi);
            }

            var npmiDicWriter = new StreamWriter("npmi.tsv");
            foreach (var elem in nPMIDic)
            {
                npmiDicWriter.Write(elem.Key.Item1.Trim());
                npmiDicWriter.Write("\t");
                npmiDicWriter.Write(elem.Key.Item2.Trim());
                npmiDicWriter.Write("\t");
                npmiDicWriter.Write(elem.Value);
                npmiDicWriter.WriteLine();
            }
            npmiDicWriter.Close();

            //var npmiList = nPMIDic
            //    .OrderByDescending(e => e.Value)
            //    .Take(100)
            //    .ToList();

            //foreach (var item in npmiList)
            //{
            //    Console.WriteLine(item.Key.Item1 + item.Key.Item2 + item.Value);
            //}

            npmiRecommendItemSet = new HashSet<Tuple<string, string>>(nPMIDic
                .Where(e => e.Value >= 0.4)
                .Select(e => e.Key));
        }
        public void train(int topN)
        {
            _topN = topN;

            BestSellerRecommendList = ArrayView<string>.Of(trainData
                .GroupBy(e => e.ISBN)
                .Select(e => new { ISBN = e.Key, Count = e.Count() })
                .OrderByDescending(e => e.Count)
                .Select(e => e.ISBN)
                .Take(topN));

            isbnSellCountList = trainData
                .GroupBy(e => e.ISBN)
                .Select(e => new { ISBN = e.Key, Count = e.Count() })
                .OrderByDescending(e => e.Count)
                .Select(e => Tuple.Create(e.ISBN, e.Count))
                .ToList();

            isbnSellCountDic = isbnSellCountList.ToDictionary(e => e.Item1, e => e.Item2);

            userISBNDic = trainData
                .GroupBy(e => e.회원번호)
                .Select(e => new { UserId = e.Key, ISBNSet = new HashSet<string>(e.Select(t => t.ISBN)) })
                .ToDictionary(e => e.UserId, e => e.ISBNSet);

            foreach (var userA in userISBNDic.Keys)
            {
                simUserWeightDic.Add(userA, new List<Tuple<string, double>>());

                foreach (var userB in userISBNDic.Keys)
                {
                    var sim = simularity(userISBNDic[userA], userISBNDic[userB]);
                    if (sim == 0.0) continue;

                    simUserWeightDic[userA].Add(Tuple.Create(userB, sim));
                }
            }
        }