public void Confirm() { Parallel.For(0, NeuronCount, i => { var cell = Cells[i] as Cell; for (int j = 0; j < Cells.Count; j++) { if (i == j) { continue; } if (cell.Location.DistanceTo(Cells[j].Location) < cell.AxsonLength) { if (Cells[j] is Cell) { if ((Cells[j] as Cell).AxsonConnectedID.Contains(cell.ID)) { continue; } } cell.AddAxson(Cells[j].ID); } } }); Cells.Sort((c1, c2) => { if (c1.ID > c2.ID) { return 1; } else if (c1.ID < c2.ID) { return -1; } else { return 0; } }); int len = Cells.Sum(x => (x is Cell) ? (x as Cell).AxsonConnectedID.Count : 0); axsonConnectMatrix = new RNdArray(len); axsonConnectCount = new RNdArray(Cells.Count); axsonConnectStartIndex = new RNdArray(Cells.Count); int index = 0; for (int i = 0; i < Cells.Count; i++) { if (Cells[i] is Cell) { var cell = (Cells[i] as Cell); axsonConnectCount[i] = cell.AxsonConnectedID.Count; for (int j = 0; j < cell.AxsonConnectedID.Count; j++) { axsonConnectMatrix[index] = cell.AxsonConnectedID[j]; index++; } } else { axsonConnectCount[i] = 0; } } Parallel.For(0, Cells.Count, i => { axsonConnectStartIndex[i] = Gpgpu.FunctionBase.StartPosition(i, new Components.GPGPU.Function.ComputeParameter("", axsonConnectCount, State.MemoryModeSet.WriteOnly)); }); var random = new Random(); cellValue = new RNdArray(Cells.Count); cellSignal = new RNdArray(Cells.Count); cellActivity = new RNdArray(Cells.Count); cellState = new RNdArray(Cells.Count); connectWeight = new RNdArray(axsonConnectMatrix.Length); cellEnergy = new RNdArray(Cells.Count); Parallel.For(0, connectWeight.Length, i => { connectWeight[i] = 1;// random.NextDouble(); }); var weightInitialize = new Gpgpu.Function.WeightInitialize(); weightInitialize.FunctionConfiguration(); Components.GPGPU.ComputeVariable variable; variable = new Components.GPGPU.ComputeVariable(); variable.Add("ConnectWeight", connectWeight, State.MemoryModeSet.WriteOnly); variable.Add("AxsonConnectCount", axsonConnectCount, State.MemoryModeSet.ReadOnly); variable.Add("axsonConnectStartIndex", axsonConnectStartIndex, State.MemoryModeSet.ReadOnly); variable.Add("AxsonConnectMatrix", axsonConnectMatrix, State.MemoryModeSet.ReadOnly); variable.Argument.Add(new Components.GPGPU.ComputeVariable.ValueSet("CellCount") { Value = cellValue.Length }); variable.Argument.Add(new Components.GPGPU.ComputeVariable.ValueSet("NeuronCount") { Value = NeuronCount }); weightInitialize.Do(false, variable); foreach (var item in Receptors) { item.Start(); } DoProcess(); }
private void DoProcess() { Energy = NeuronCount / 2; new System.Threading.Thread(() => { double prevEnergy = Energy; var resValue = new RNdArray(Cells.Count); var resSignal = new RNdArray(Cells.Count); var resActivity = new RNdArray(Cells.Count); var resState = new RNdArray(Cells.Count); Components.GPGPU.ComputeVariable variable; var function = new Gpgpu.Function.FieldUpdateStep(); function.FunctionConfiguration(); while (!CoreObject.IsTerminate) { Energy = 1000; float d_energy = 1;// (float)(Energy - prevEnergy); DateTime start = DateTime.Now; cellValue.CopyBy(Cells.Select(x => x.Value).ToArray()); cellSignal.CopyBy(Cells.Select(x => x.Signal).ToArray()); cellActivity.CopyBy(Cells.Select(x => (x is Cell) ? (Real)(x as Cell).Activity : x.Signal).ToArray()); cellState.CopyBy(Cells.Select(x => (x is Cell) ? (Real)(int)(x as Cell).State : 1).ToArray()); variable = new Components.GPGPU.ComputeVariable(); variable.Add("cellValue", cellValue, State.MemoryModeSet.ReadOnly); variable.Add("cellSignal", cellSignal, State.MemoryModeSet.ReadOnly); variable.Add("cellActivity", cellActivity, State.MemoryModeSet.ReadOnly); variable.Add("cellState", cellState, State.MemoryModeSet.ReadOnly); variable.Add("connectWeight", connectWeight, State.MemoryModeSet.ReadOnly); variable.Add("cellEnergy", cellEnergy, State.MemoryModeSet.WriteOnly); variable.Add("axsonConnectCount", axsonConnectCount, State.MemoryModeSet.ReadOnly); variable.Add("axsonConnectStartIndex", axsonConnectStartIndex, State.MemoryModeSet.ReadOnly); variable.Add("axsonConnectMatrix", axsonConnectMatrix, State.MemoryModeSet.ReadOnly); variable.Add("resValue", resValue, State.MemoryModeSet.WriteOnly); variable.Add("resSignal", resSignal, State.MemoryModeSet.WriteOnly); variable.Add("resActivity", resActivity, State.MemoryModeSet.WriteOnly); variable.Add("resState", resState, State.MemoryModeSet.WriteOnly); variable.Argument.Add(new Components.GPGPU.ComputeVariable.ValueSet("CellCount") { Value = cellValue.Length }); variable.Argument.Add(new Components.GPGPU.ComputeVariable.ValueSet("NeuronCount") { Value = NeuronCount }); variable.Argument.Add(new Components.GPGPU.ComputeVariable.ValueSet("Energy") { Value = Energy }); variable.Argument.Add(new Components.GPGPU.ComputeVariable.ValueSet("dEnergy") { Value = d_energy }); function.Do(false, variable); #region /* need to GPGPU */ foreach (var item in Signals) { item.Signal = 0; foreach (var id in item.AxsonConnectedID) { item.Signal += Cells[id].Value; } item.Signal /= item.AxsonConnectedID.Count; } #endregion SignalUpdateEvent?.Invoke(new FieldState() { Energy = this.Energy, Signals = new List<double>(Signals.Select(x => (double)x.Signal).ToArray()), Locations = new List<Location>(Signals.Select(x => x.Location).ToArray()), }); #if false Parallel.For(0, NeuronCount, i => { #else for (int i = 0; i < NeuronCount; i++) { #endif if (Cells[i] is Cell) { var cell = (Cells[i] as Cell); cell.Value = resValue[i]; cell.Signal = resSignal[i]; cell.Activity = resActivity[i]; cell.State = (Cell.IgnitionState)Enum.ToObject(typeof(Cell.IgnitionState), (int)resState[i]); cell.Energy = cellEnergy[i]; } } #if false ); #endif stepCount++; stepTime = (stepTime + function.StepElapsedSpan.TotalMilliseconds) / 2; totalStepTime = (totalStepTime + (DateTime.Now - start).TotalMilliseconds) / 2; System.Threading.Thread.Sleep(CoreObject.Interval); } }).Start(); }
public InitializeNeuron(int count, double area, double axsonLengthBase) { var random = new Random(); var list = new List <Location>(); Components.RNdArray px = new Components.RNdArray(count); Components.RNdArray py = new Components.RNdArray(count); Components.RNdArray pz = new Components.RNdArray(count); Components.RNdArray axson = new Components.RNdArray(count); for (int i = 0; i < count; i++) { var loc = new Location(random, area); list.Add(loc); px[i] = loc.X; py[i] = loc.Y; pz[i] = loc.Z; axson[i] = (axsonLengthBase) + (axsonLengthBase / 10.0) * (random.NextDouble() * 2 - 1); } Components.RNdArray hasRef = new Components.RNdArray(count); bool check = false; var function = new Connectome.Gpgpu.Function.LocationInitializeStep(); function.FunctionConfiguration(); int repetitioncount = 0; while (!check) { var variable = new Components.GPGPU.ComputeVariable(); variable.Add("px", px, Components.State.MemoryModeSet.ReadOnly); variable.Add("py", py, Components.State.MemoryModeSet.ReadOnly); variable.Add("pz", pz, Components.State.MemoryModeSet.ReadOnly); variable.Add("paxson", axson, Components.State.MemoryModeSet.ReadOnly); variable.Add("phasRef", hasRef, Components.State.MemoryModeSet.WriteOnly); variable.Argument.Add(new Components.GPGPU.ComputeVariable.ValueSet("count") { Value = count }); variable.Argument.Add(new Components.GPGPU.ComputeVariable.ValueSet("connectcount") { Value = 2 }); function.Do(false, variable); int containscount = hasRef.Data.Count(x => x.Value == 0); if (containscount != 0) { repetitioncount++; for (int i = 0; i < count; i++) { if (hasRef.Data[i] == 0) { //var loc = new Location(random, axson[i]) + list[random.Next(count)]; var loc = new Location(random, area); list[i] = loc; px[i] = loc.X; py[i] = loc.Y; pz[i] = loc.Z; } } } else { check = true; } } for (int i = 0; i < count; i++) { NeuronSources.Add(new NeuronSource(list[i], axson[i])); } //NeuronSources.Sort((l1, l2) => //{ // var def = new Location(); // var dist1 = l1.Location.DistanceTo(def); // var dist2 = l2.Location.DistanceTo(def); // if (dist1 >= dist2) // { // return 1; // } // else if (dist1 <= dist2) // { // return -1; // } // else { return 0; } //}); }