public void Run(PsoParameters psoParameters, IParticle[] proxyParticleServices = null) { _function = FunctionFactory.GetFitnessFunction(psoParameters.FunctionParameters); var useGpu = psoParameters.GpuParameters.UseGpu && GpuController.AnySupportedGpu(); CudaParticle cudaParticle = null; if (useGpu) { cudaParticle = PrepareCudaAlgorithm(psoParameters); } var particles = PrepareParticles(psoParameters, proxyParticleServices, cudaParticle); RunningParameters = psoParameters; _algorithm = new PsoAlgorithm(psoParameters, _function, particles.ToArray()); _cudaReadyLock = new AutoResetEvent(false); if (useGpu) { RunningCudaAlgorithm = Task <ParticleState> .Factory.StartNew(() => { _cudaAlgorithm.Initialize(); _cudaReadyLock.Set(); var result = _cudaAlgorithm.Run(_cudaTokenSource.Token); _function.Evaluate(result.Location); _cudaAlgorithm.Dispose(); return(result); }, _cudaTokenSource.Token); } else { _cudaReadyLock.Set(); } RunningAlgorithm = Task <ParticleState> .Factory.StartNew(delegate { _cudaReadyLock.WaitOne(); _cudaReadyLock.Dispose(); return(StartAlgorithm(_tokenSource.Token)); }, _tokenSource.Token); }
private List <IParticle> PrepareParticles(PsoParameters psoParameters, IParticle[] proxyParticleServices, CudaParticle cudaParticle) { var particles = CreateParticles(psoParameters, _function); if (proxyParticleServices != null) { particles.AddRange(proxyParticleServices); } if (cudaParticle != null) { particles.Add(cudaParticle); } return(particles); }