public void SwapTest() { var a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; a.Swap(0, 1); Assert.AreEqual(a[0], 2); Assert.AreEqual(a[1], 1); var b = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var flat = new FlattArray <int>(b, 3); flat.Swap(0, 1); Assert.AreEqual(4, flat[0, 0]); Assert.AreEqual(5, flat[0, 1]); Assert.AreEqual(6, flat[0, 2]); Assert.AreEqual(1, flat[1, 0]); Assert.AreEqual(2, flat[1, 1]); Assert.AreEqual(3, flat[1, 2]); }
public void SortTest() { var data = new float[, ] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 }, }; var classes = new int[] { 1, 2, 3, 4 }; var sortBy = new float[] { 2, 0, 1, 3 }; var flat = new FlattArray <float>(data); var cuda = new CudaDataSet() { Classes = classes, Vectors = flat }; var sorted = Sorting.Sort(cuda, sortBy); Assert.AreEqual(sorted.Vectors[0, 0], cuda.Vectors[1, 0]); Assert.AreEqual(sorted.Classes[0], cuda.Classes[1]); Assert.AreEqual(sorted.Vectors[1, 0], cuda.Vectors[2, 0]); Assert.AreEqual(sorted.Classes[1], cuda.Classes[2]); Assert.AreEqual(sorted.Vectors[2, 0], cuda.Vectors[0, 0]); Assert.AreEqual(sorted.Classes[2], cuda.Classes[0]); }
public void FlatArrayTest() { var a = new float[, ] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } }; var flat = new FlattArray <float>(a); Assert.AreEqual(a[1, 1], flat.Get(1, 1)); Assert.AreEqual(a[0, 2], flat.Get(0, 2)); Assert.AreEqual(a[2, 1], flat.Get(2, 1)); var b = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 5 }; var flat2 = new FlattArray <float>(b, 3); Assert.AreEqual(a.GetLength(0), 4); Assert.AreEqual(a.GetLength(1), 3); Assert.AreEqual(flat2[0, 0], b[0]); Assert.AreEqual(flat2[0, 1], b[1]); Assert.AreEqual(flat2[3, 2], b[11]); }
public static void Swap <T>(this FlattArray <T> arr, int row1, int row2) { var tmpVector = new T[arr.GetLength(1)]; Array.Copy( arr.Raw, arr.GetLength(1) * row1, tmpVector, 0, arr.GetLength(1) ); Array.Copy( arr.Raw, arr.GetLength(1) * row2, arr.Raw, arr.GetLength(1) * row1, arr.GetLength(1) ); Array.Copy( tmpVector, 0, arr.Raw, arr.GetLength(1) * row2, arr.GetLength(1) ); }
private static CudaDataSet <T> copyResult <T>(CudaDataSet <T> src, int[] sorted) { FlattArray <float> newArr = new FlattArray <float>(src.Vectors.GetLength(0), src.Vectors.GetLength(1)); T[] newVal = new T[src.Vectors.GetLength(0)]; int attrCount = src.Vectors.GetLength(1); Parallel.For(0, sorted.Length, i => { Array.Copy( src.Vectors.Raw, sorted[i] * attrCount, newArr.Raw, i * attrCount, attrCount ); newVal[i] = src.Classes[sorted[i]]; }); return(new CudaDataSet <T>() { Vectors = newArr, Classes = newVal }); }
public static FlattArray <T>[] Split <T>(FlattArray <T> data, float[] parts) { var splited = new FlattArray <T> [parts.Length]; int startIndex = 0; for (int i = 0; i < parts.Length; i++) { int endIndex = startIndex + (int)Math.Round(data.GetLength(0) * parts[i]); if (endIndex >= data.GetLength(0)) { endIndex = data.GetLength(0); } int len = endIndex - startIndex; T[] arr = new T[len * data.GetLength(1)]; Array.Copy(data.Raw, startIndex * data.GetLength(1), arr, 0, len * data.GetLength(1) ); startIndex = endIndex; splited[i] = new FlattArray <T>(arr, data.GetLength(1)); } return(splited); }
public static float ColumnAvrage(FlattArray <float> data, int column) { float avg = 0; for (int i = 0; i < data.GetLength(0); i++) { avg += data[i, column]; } return(avg / data.GetLength(0)); }
public subject FindFitest() { float[] hostFitness = deviceFitnes; int[] indeces = fitnessIndeces; var a = new FlattArray <byte>((byte[])populationGens, genLength).To2d(); return(__findFitest( deviceFitnes.DevicePointer.Pointer, fitnessIndeces.DevicePointer.Pointer, fitnessIndeces.Size )); }
private int[] ApplyRest(CudaContext context, CudaDataSet <int> data) { int vectorsCount = data.Vectors.GetLength(0); int attributeCount = data.Vectors.GetLength(1); var kernel = context.LoadKernel("kernels/drop3.ptx", "findNeighbours"); kernel.GridDimensions = data.Vectors.GetLength(0) / ThreadsPerBlock + 1; kernel.BlockDimensions = ThreadsPerBlock; using (CudaDeviceVariable <int> d_classes = data.Classes) using (CudaDeviceVariable <float> vectors = data.Vectors.Raw) using (var heapMemory = new CudaDeviceVariable <HeapData>(data.Vectors.GetLength(0) * CasheSize)) using (var nearestEnemyDistances = new CudaDeviceVariable <float>(data.Vectors.GetLength(0))) { kernel.Run( vectors.DevicePointer, data.Vectors.GetLength(0), data.Vectors.GetLength(1), CasheSize, d_classes.DevicePointer, heapMemory.DevicePointer, nearestEnemyDistances.DevicePointer ); float[] hostNearestEnemy = nearestEnemyDistances; float[][] hostVectors = data.Vectors.To2d(); var Neighbors = new FlattArray <HeapData>(heapMemory, CasheSize); var nearestNeighbors = new int[vectorsCount][]; for (int i = 0; i < vectorsCount; i++) { nearestNeighbors[i] = new int[CasheSize]; for (int j = 0; j < CasheSize; j++) { nearestNeighbors[i][j] = Neighbors[i, j].label; } } HostDataset host = data.ToHostDataSet(); SortDataDesc(host, nearestNeighbors, hostNearestEnemy); return(proccesData(context, host, nearestNeighbors)); } }
//static float Knn(CudaDataSet teaching, CudaDataSet test, int k = 3, int threadsPerBlock = 256) //{ // CudaContext context = new CudaContext(); // var kernel = context.LoadKernel("kernels/kernel.ptx", "knnKernal"); // kernel.GridDimensions = test.Vectors.GetLength(0) / threadsPerBlock + 1; // kernel.BlockDimensions = threadsPerBlock; // CudaDeviceVariable<float> teachingDevice = teaching.Vectors.Raw; // CudaDeviceVariable<float> testDevice = test.Vectors.Raw; // CudaDeviceVariable<int> labelDevice = teaching.Classes; // CudaDeviceVariable<int> testLabels = test.Classes; // CudaDeviceVariable<HeapData> heapMemory = // new CudaDeviceVariable<HeapData>(test.Vectors.GetLength(0) * k); // heapMemory.Memset(uint.MaxValue); // kernel.Run( // teachingDevice.DevicePointer,//teaching vectors // teaching.Vectors.GetLength(0),//teaching count // testDevice.DevicePointer,//testVectors // test.Vectors.GetLength(0),//test count // labelDevice.DevicePointer,//labels // test.Vectors.GetLength(1),//vectorLen // k, // heapMemory.DevicePointer // ); // FlattArray<HeapData> maxL = // new FlattArray<HeapData>(heapMemory, k); // int failureCount = 0; // for (int i = 0; i < maxL.GetLength(0); i++) // { // var labelCounts = new Dictionary<int, int>(); // for (int j = 0; j < maxL.GetLength(1); j++) // { // if (labelCounts.ContainsKey(maxL[i, j].label)) // { // labelCounts[maxL[i, j].label]++; // } // else // { // labelCounts[maxL[i, j].label] = 1; // } // } // var guesedLabel = labelCounts.Aggregate((max, current) => // { // if (current.Value > max.Value) // { // return current; // } // else // { // return max; // } // }).Key; // if (guesedLabel != test.Classes[i]) // { // failureCount++; // } // } // context.Dispose(); // return (float)(test.Vectors.GetLength(0) - failureCount) / (float)test.Vectors.GetLength(0); //} public static FlattArray <byte> CreateRandomPopulation(int popSize, int genLength) { FlattArray <byte> population = new FlattArray <byte>(popSize, genLength); Random r = new Random(); for (int i = 0; i < population.GetLength(0); i++) { for (int j = 0; j < population.GetLength(1); j++) { population[i, j] = r.NextDouble() < 0.5 ? (byte)1 : (byte)0; } } return(population); }
public void VarianceTest() { var data = new float[, ] { { 1, 1, 1 }, { 2, 1, 5 }, { 3, 1, 8 } }; var flat = new FlattArray <float>(data); var variances = DataSetHelper.Variances(flat); Assert.AreEqual((float)0.66666666666667, variances[0]); Assert.AreEqual((float)0, variances[1]); Assert.AreEqual((float)8.2222222222222, variances[2]); }
public NeighborFinder(CudaContext context, FlattArray <float> vectors, int countToFind) { heap = new Data[countToFind]; this.vectorCount = vectors.GetLength(0); this.attrCount = vectors.GetLength(1); this.context = context; kernel = context.LoadKernel("kernels/drop3.ptx", "calculateDistances"); kernel.GridDimensions = vectors.GetLength(0) / 256 + 1; kernel.BlockDimensions = 256; results = new float[vectors.GetLength(0)]; deviceVectors = vectors.Raw; deviceResult = new CudaDeviceVariable <float>(vectors.GetLength(0)); deviceIsInDataSet = new CudaDeviceVariable <byte>(vectors.GetLength(0)); }
public static float[] Variances(FlattArray <float> vectors) { float[] variances = new float[vectors.GetLength(1)]; Parallel.For(0, vectors.GetLength(1), col => { float avrage = ColumnAvrage(vectors, col); float[] diffrencesSquared = new float[vectors.GetLength(0)]; for (int row = 0; row < vectors.GetLength(0); row++) { float f = vectors[row, col] - avrage; diffrencesSquared[row] = f * f; } variances[col] = diffrencesSquared.Average(); }); return(variances); }
public static FlattArray <byte> CreatePopulationBasedOnParent(byte[] parent, int popSize, float flippChance, float bias) { FlattArray <byte> population = new FlattArray <byte>(popSize, parent.Length); Random r = new Random(); for (int i = 0; i < population.GetLength(0); i++) { for (int j = 0; j < population.GetLength(1); j++) { float chance = population[i, j] == 1 ? flippChance : flippChance - bias; population[i, j] = r.NextDouble() < chance ? (byte)1 : (byte)0; } } return(population); }
public static T[][] To2d <T>(this FlattArray <T> arr) { T[][] res = new T[arr.GetLength(0)][]; int colCount = arr.GetLength(1); for (int i = 0; i < res.Length; i++) { res[i] = new T[colCount]; Array.Copy( arr.Raw, i * colCount, res[i], 0, colCount ); } return(res); }
public static FlattArray <T> Filter <T>( this FlattArray <T> toFilter, int[] indexesToStay) { int colCount = toFilter.GetLength(1); T[] newData = new T[indexesToStay.Length * colCount]; for (int i = 0; i < indexesToStay.Length; i++) { Array.Copy( toFilter.Raw, indexesToStay[i] * colCount, newData, i * colCount, colCount ); } return(new FlattArray <T>(newData, colCount)); }
public void FlatArrayTo2dTest() { var a = new float[, ] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } }; var flat = new FlattArray <float>(a); var twoD = flat.To2d(); for (int i = 0; i < flat.GetLength(0); i++) { for (int j = 0; j < flat.GetLength(1); j++) { Assert.AreEqual(flat[i, j], twoD[i][j]); } } }
public void FlatArrayJaggeredConstructorTest() { float[][] data = new float[][] { new float[] { 2, 5, 4, 4 }, new float[] { -5, 84, 8, 8 }, new float[] { -644, 12, 8, 9 }, new float[] { 156, 64, 8, 54 }, new float[] { 2, 456, 8, 4 }, }; FlattArray <float> flat = new FlattArray <float>(data); for (int i = 0; i < data.Length; i++) { for (int j = 0; j < data[i].Length; j++) { Assert.AreEqual(flat[i, j], data[i][j]); } } }
public Evolutionary2( CudaContext context, IFitnessFunction fitnessCalc, FlattArray <byte> initialPopulation ) { this.context = context; this.popSize = initialPopulation.GetLength(0); this.genLength = initialPopulation.GetLength(1); int alignedPopSizeMemory = (popSize * genLength) + ((popSize * genLength) % (sizeof(int))); populationGens = new CudaDeviceVariable <byte>(alignedPopSizeMemory); populationGens2 = new CudaDeviceVariable <byte>(alignedPopSizeMemory); context.CopyToDevice(populationGens2.DevicePointer, initialPopulation.Raw); //initialPopulation.Raw; deviceFitnes = new CudaDeviceVariable <float>(popSize); fitnessIndeces = new CudaDeviceVariable <int>(popSize); LoadKernels(); MutationRate = 0.01f; CrossOverRate = 0.7f; Alpha = 0.7f; Elitism = 0.2f; this.fitnessCalc = fitnessCalc; performGeneticAlgorythm.SetConstantVariable("popSize", popSize); performGeneticAlgorythm.SetConstantVariable("genLength", genLength); }
public void FlatArrayFilterTest() { var a = new float[, ] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } }; var flat = new FlattArray <float>(a); { var filtered = flat.Filter(new int[] { }); Assert.AreEqual(0, filtered.GetLength(0)); Assert.AreEqual(3, filtered.GetLength(1)); } { var filtered = flat.Filter(new int[] { 0, 3 }); Assert.AreEqual(2, filtered.GetLength(0)); Assert.AreEqual(3, filtered.GetLength(1)); Assert.AreEqual(flat[0, 0], filtered[0, 0]); Assert.AreNotEqual(flat[1, 0], filtered[1, 0]); } }