public override void Run(MatOperation operation, MyMemoryBlock <float> A, MyMemoryBlock <float> B, MyMemoryBlock <float> Result) { switch (operation) { case MatOperation.Multiplication: // vectors/matrices have to be always in the correct dimesions! if (A.Count == 1) // valueA * B { Result.Fill(.0f); A.SafeCopyToHost(); MyCublasFactory.Instance.Axpy(A.Host[0], B.GetDevice(callee), 1, Result.GetDevice(callee), 1); } else if (B.Count == 1) // A * valueB { Result.Fill(.0f); B.SafeCopyToHost(); MyCublasFactory.Instance.Axpy(B.Host[0], A.GetDevice(callee), 1, Result.GetDevice(callee), 1); } else // another executions... { Run(operation, A.GetDevice(callee), A.Count, A.ColumnHint, B.GetDevice(callee), B.Count, B.ColumnHint, Result.GetDevice(callee), Result.Count, Result.ColumnHint, 0); } break; case MatOperation.DotProd: Run(operation, A.GetDevice(callee), A.Count, A.ColumnHint, B.GetDevice(callee), B.Count, B.ColumnHint, Result.GetDevice(callee), Result.Count, Result.ColumnHint, 0); break; default: MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation"); break; } }
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]); } }
public override void CopyToMemoryBlock(MyMemoryBlock <T> destination, int srcOffset, int destOffset, int count) { int size = Marshal.SizeOf(typeof(T)); if (destination is MyTemporalMemoryBlock <T> ) { destination.GetDevice(Owner.GPU).CopyToDevice(Device[Owner.GPU], (TimeOffset + srcOffset) * size, (TimeOffset + destOffset) * size, count * size); } else { destination.GetDevice(Owner.GPU).CopyToDevice(Device[Owner.GPU], (TimeOffset + srcOffset) * size, destOffset * size, count * size); } }
public override void CopyFromMemoryBlock(MyMemoryBlock <T> source, int srcOffset, int destOffset, int count) { int size = Marshal.SizeOf(typeof(T)); if (source is MyTemporalMemoryBlock <T> ) { Device[Owner.GPU].CopyToDevice(source.GetDevice(Owner.GPU), (TimeOffset + srcOffset) * size, (TimeOffset + destOffset) * size, count * size); } else { Device[Owner.GPU].CopyToDevice(source.GetDevice(Owner.GPU), srcOffset * size, (TimeOffset + destOffset) * size, count * size); } }
public override void Run(MatOperation operation, MyMemoryBlock <float> A) { switch (operation) { case MatOperation.Minus: MyCublasFactory.Instance.Scale(-1.0f, A.GetDevice(callee), 1); break; case MatOperation.Normalize: float nrm = MyCublasFactory.Instance.Norm2(A.GetDevice(callee), 1); MyCublasFactory.Instance.Scale(1 / nrm, A.GetDevice(callee), 1); break; default: MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation"); break; } }
public override void Run(MatOperation operation, MyMemoryBlock <float> A, MyMemoryBlock <float> Result) { int itmp; Result.Fill(.0f); switch (operation) { case MatOperation.AbsMinIndex: itmp = MyCublasFactory.Instance.Min(A.GetDevice(callee), 1); Result.Fill((float)(itmp - 1)); break; case MatOperation.AbsMaxIndex: itmp = MyCublasFactory.Instance.Max(A.GetDevice(callee), 1); Result.Fill((float)(itmp - 1)); break; case MatOperation.Norm2: MyCublasFactory.Instance.Norm2(A.GetDevice(callee), 1, Result.GetDevice(callee)); break; case MatOperation.Normalize: float nrm = MyCublasFactory.Instance.Norm2(A.GetDevice(callee), 1); MyCublasFactory.Instance.Axpy(1 / nrm, A.GetDevice(callee), 1, Result.GetDevice(callee), 1); break; case MatOperation.Minus: MyCublasFactory.Instance.Axpy(-1.0f, A.GetDevice(callee), 1, Result.GetDevice(callee), 1); break; case MatOperation.Copy: MyCublasFactory.Instance.Copy(A.GetDevice(callee), 1, Result.GetDevice(callee), 1); break; default: MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation"); break; } }
public override void Run(MatOperation operation, MyMemoryBlock <float> A, float value, MyMemoryBlock <float> Result) { Result.Fill(.0f); switch (operation) { case MatOperation.Multiplication: MyCublasFactory.Instance.Axpy(value, A.GetDevice(callee), 1, Result.GetDevice(callee), 1); break; default: MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation"); break; } }
public override void Run(MatOperation operation, MyMemoryBlock<float> A, float value, MyMemoryBlock<float> Result) { Result.Fill(.0f); switch (operation) { case MatOperation.Multiplication: MyCublasFactory.Instance.Axpy(value, A.GetDevice(callee), 1, Result.GetDevice(callee), 1); break; default: MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation"); break; } }
public override void Run(MatOperation operation, MyMemoryBlock<float> A) { switch (operation) { case MatOperation.Minus: MyCublasFactory.Instance.Scale(-1.0f, A.GetDevice(callee), 1); break; case MatOperation.Normalize: float nrm = MyCublasFactory.Instance.Norm2(A.GetDevice(callee), 1); MyCublasFactory.Instance.Scale(1 / nrm, A.GetDevice(callee), 1); break; default: MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation"); break; } }
public override void Run(MatOperation operation, MyMemoryBlock<float> A, MyMemoryBlock<float> Result) { int itmp; Result.Fill(.0f); switch (operation) { case MatOperation.MinIndex: itmp = MyCublasFactory.Instance.Min(A.GetDevice(callee), 1); Result.Fill((float)(itmp - 1)); break; case MatOperation.MaxIndex: itmp = MyCublasFactory.Instance.Max(A.GetDevice(callee), 1); Result.Fill((float)(itmp - 1)); break; case MatOperation.Norm2: MyCublasFactory.Instance.Norm2(A.GetDevice(callee), 1, Result.GetDevice(callee)); break; case MatOperation.Normalize: float nrm = MyCublasFactory.Instance.Norm2(A.GetDevice(callee), 1); MyCublasFactory.Instance.Axpy(1 / nrm, A.GetDevice(callee), 1, Result.GetDevice(callee), 1); break; case MatOperation.Minus: MyCublasFactory.Instance.Axpy(-1.0f, A.GetDevice(callee), 1, Result.GetDevice(callee), 1); break; case MatOperation.Copy: MyCublasFactory.Instance.Copy(A.GetDevice(callee), 1, Result.GetDevice(callee), 1); break; default: MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation"); break; } }
public override void Run(MatOperation operation, MyMemoryBlock<float> A, MyMemoryBlock<float> B, MyMemoryBlock<float> Result) { switch (operation) { case MatOperation.Multiplication: // vectors/matrices have to be always in the correct dimesions! if (A.Count == 1) // valueA * B { Result.Fill(.0f); A.SafeCopyToHost(); MyCublasFactory.Instance.Axpy(A.Host[0], B.GetDevice(callee), 1, Result.GetDevice(callee), 1); } else if (B.Count == 1) // A * valueB { Result.Fill(.0f); B.SafeCopyToHost(); MyCublasFactory.Instance.Axpy(B.Host[0], A.GetDevice(callee), 1, Result.GetDevice(callee), 1); } else // another executions... { Run(operation, A.GetDevice(callee), A.Count, A.ColumnHint, B.GetDevice(callee), B.Count, B.ColumnHint, Result.GetDevice(callee), Result.Count, Result.ColumnHint, 0); } break; case MatOperation.DotProd: Run(operation, A.GetDevice(callee), A.Count, A.ColumnHint, B.GetDevice(callee), B.Count, B.ColumnHint, Result.GetDevice(callee), Result.Count, Result.ColumnHint, 0); break; default: MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation"); break; } }
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); }
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); }
public override void Execute() { currentGen++; // If not genetically training. Return //Get first population member from the network getFFWeights(population[0]); population[0].SafeCopyToDevice(); if (!DirectEvolution) { MyCublasFactory.Instance.Gemm(Operation.NonTranspose, Operation.NonTranspose, arr_size, arr_size, arr_size, 1.0f, multiplier.GetDevice(Owner), arr_size, population[0].GetDevice(Owner), arr_size, 0.0f, outputPop[0].GetDevice(Owner), arr_size ); MyCublasFactory.Instance.Gemm(Operation.NonTranspose, Operation.Transpose, arr_size, arr_size, arr_size, 1.0f, outputPop[0].GetDevice(Owner), arr_size, multiplier.GetDevice(Owner), arr_size, 0.0f, population[0].GetDevice(Owner), arr_size ); } //Read the saved coeffs from the initial weight matrix into the first chromosome population[0].CopyToMemoryBlock(cudaMatrices, 0, 0, arr_size * arr_size); m_extractKernel.SetupExecution(1); m_extractKernel.Run(cudaMatrices, chromosomePop, CoefficientsSaved, arr_size); // Recombine and grow the population if (DirectEvolution) { m_geneticKernel.Run(cudaMatrices, arr_size, m_weights, Owner.PopulationSize, chromosomePop, noise, Owner.MutationRate, Owner.Survivors, fitnesses, marking, WeightMagnitude); } else { m_geneticKernel.Run(cudaMatrices, arr_size, CoefficientsSaved, Owner.PopulationSize, chromosomePop, noise, Owner.MutationRate, Owner.Survivors, fitnesses, marking, Alpha); } chromosomePop.SafeCopyToHost(); cudaMatrices.Fill(0.0f); m_implantKernel.SetupExecution(Owner.PopulationSize); m_implantKernel.Run(cudaMatrices, chromosomePop, CoefficientsSaved, arr_size); for (int i = 0; i < Owner.PopulationSize; i++) { // Read the cudaMatrices into the population population[i].CopyFromMemoryBlock(cudaMatrices, i * arr_size * arr_size, 0, arr_size * arr_size); if (!DirectEvolution) { MyCublasFactory.Instance.Gemm(Operation.Transpose, Operation.NonTranspose, arr_size, arr_size, arr_size, 1.0f, multiplier.GetDevice(Owner), arr_size, population[i].GetDevice(0), arr_size, 0.0f, outputPop[i].GetDevice(0), arr_size ); MyCublasFactory.Instance.Gemm(Operation.NonTranspose, Operation.NonTranspose, arr_size, arr_size, arr_size, 1.0f, outputPop[i].GetDevice(0), arr_size, multiplier.GetDevice(Owner), arr_size, 0.0f, population[i].GetDevice(0), arr_size ); } population[i].SafeCopyToHost(); noise.Host[i] = (float)m_rand.NextDouble(); } noise.SafeCopyToDevice(); // Determine the fitness of each member determineFitnesses(); chromosomePop.SafeCopyToHost(); #region Sort Chromosomes //sort the chromosomes and populations by fitness //bubble sort, can be improved float tmpfit; int len = Owner.PopulationSize; int newlen; while (len != 0) { newlen = 0; for (int i = 1; i < len; i++) { if (fitnesses.Host[i - 1] < fitnesses.Host[i]) { // Swap fitnesses on the host tmpfit = fitnesses.Host[i - 1]; fitnesses.Host[i - 1] = fitnesses.Host[i]; fitnesses.Host[i] = tmpfit; newlen = i; // Swap Chromosomes on the device for (int x = 0; x < CoefficientsSaved; x++) { tmpfit = chromosomePop.Host[i * CoefficientsSaved + x]; chromosomePop.Host[i * CoefficientsSaved + x] = chromosomePop.Host[(i - 1) * CoefficientsSaved + x]; chromosomePop.Host[(i - 1) * CoefficientsSaved + x] = tmpfit; } for (int x = 0; x < arr_size * arr_size; x++) { tmpfit = population[i - 1].Host[x]; population[i - 1].Host[x] = population[i].Host[x]; population[i].Host[x] = tmpfit; } } } len = newlen; } MyLog.INFO.WriteLine("Top {0} networks:", Math.Max(Owner.Survivors, Owner.PopulationSize / 10)); for (int i = 0; i < Math.Max(Owner.Survivors, Owner.PopulationSize / 10); i++) { MyLog.INFO.Write("Fitness of network {0} is: {1}", i, fitnesses.Host[i]); if (i < Owner.Survivors) { MyLog.INFO.Write(" - surviving"); } MyLog.INFO.Write(" \n"); } #endregion // Best candidate to write to the network is the top of the population list MyLog.INFO.WriteLine("Fitness of selected network is: " + fitnesses.Host[0]); if (fitnesses.Host[0] >= Owner.TargetFitness) { MyLog.INFO.WriteLine("Found satisfying network, halting..."); Owner.Owner.SimulationHandler.PauseSimulation(); } setFFWeights(population[0]); MyLog.INFO.WriteLine("Written weights to network"); if (currentGen >= Owner.Generations && Owner.Generations > 0) { MyLog.INFO.WriteLine("Generation limit reached, halting..."); Owner.Owner.SimulationHandler.PauseSimulation(); } }
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)); }
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); }