/// <summary> /// Detects OpenCL devices. /// </summary> /// <param name="predicate"> /// The predicate to include a given device. /// </param> /// <param name="registry">The registry to add all devices to.</param> private static void GetDevicesInternal( Predicate <CLDevice> predicate, DeviceRegistry registry) { var devices = new IntPtr[MaxNumDevicesPerPlatform]; // Resolve all platforms if (!CurrentAPI.IsSupported || CurrentAPI.GetNumPlatforms(out int numPlatforms) != CLError.CL_SUCCESS || numPlatforms < 1) { return; } var platforms = new IntPtr[numPlatforms]; if (CurrentAPI.GetPlatforms(platforms, ref numPlatforms) != CLError.CL_SUCCESS) { return; } foreach (var platform in platforms) { // Resolve all devices int numDevices = devices.Length; Array.Clear(devices, 0, numDevices); if (CurrentAPI.GetDevices( platform, CLDeviceType.CL_DEVICE_TYPE_ALL, devices, out numDevices) != CLError.CL_SUCCESS) { continue; } for (int i = 0; i < numDevices; ++i) { // Resolve device and ignore invalid devices var device = devices[i]; if (device == IntPtr.Zero) { continue; } // Check for available device if (CurrentAPI.GetDeviceInfo <int>( device, CLDeviceInfoType.CL_DEVICE_AVAILABLE) == 0) { continue; } var desc = new CLDevice(platform, device); registry.Register(desc, predicate); } } }
/// <summary> /// Detects Cuda devices. /// </summary> /// <param name="predicate"> /// The predicate to include a given device. /// </param> /// <param name="registry">The registry to add all devices to.</param> private static void GetDevicesInternal( Predicate <CudaDevice> predicate, DeviceRegistry registry) { // Resolve all devices if (CurrentAPI.GetDeviceCount(out int numDevices) != CudaError.CUDA_SUCCESS || numDevices < 1) { return; } for (int i = 0; i < numDevices; ++i) { if (CurrentAPI.GetDevice(out int device, i) != CudaError.CUDA_SUCCESS) { continue; } var desc = new CudaDevice(device); if (predicate(desc)) { registry.Register(desc); } } }
/// <summary> /// Registers CPU devices. /// </summary> /// <param name="predicate"> /// The predicate to include a given device. /// </param> /// <param name="registry">The registry to add all devices to.</param> internal static void GetDevices( Predicate <CPUDevice> predicate, DeviceRegistry registry) { if (registry is null) { throw new ArgumentNullException(nameof(registry)); } if (predicate is null) { throw new ArgumentNullException(nameof(predicate)); } foreach (var desc in All) { registry.Register(desc, predicate); } }
static void Main(string[] args) { log4net.Config.BasicConfigurator.Configure(); Tracer.Initialize(new TracerManager()); AppDomain.CurrentDomain.UnhandledException += (s, e) => tracer.Error(e.ExceptionObject); TaskScheduler.UnobservedTaskException += (s, e) => tracer.Error(e.Exception); try { short port = 1055; if (args.Length > 0) port = short.Parse(args[0]); ServiceRegistration.Start(port); var devices = new DeviceRegistry(); var topics = new Dictionary<string, TopicType>(); var stream = new EventStream(); var state = new SystemState(); var impulseStore = new FileImpulseStore("Store\\Impulses"); var commandStore = new FileCommandStore("Store\\Commands"); SetupTracing(stream); // Hook up event stream consumers that perform orthogonal operations. new ClockImpulses(Clock.Default).Connect(stream); new CommandToBytes().Connect(stream); new SensedToImpulse(Sensorium.Clock.Default, topics).Connect(stream); new SetSystemState(state).Connect(stream); // Hook up stores new StoreCommands(commandStore).Connect(stream); new StoreImpulses(impulseStore).Connect(stream); var brain = new Brain(stream, devices, topics, state, Clock.Default); if (File.Exists("Server.cfg")) { var setup = Setup.Read("Server.cfg", File.ReadAllText("Server.cfg")); Console.WriteLine("Applying configuration file:"); Console.WriteLine(setup.ToString(true)); foreach (var topic in setup.Topics) { topics[topic.Key] = topic.Value; } foreach (var device in setup.DeviceTypes) { devices.Register(device.Type, device.Commands.ToArray()); } foreach (var behavior in setup.Behaviors) { brain.Behave(behavior); } } var server = new ReactiveListener(port); server.Connections.Subscribe(socket => { Console.WriteLine("New socket connected {0}", socket.GetHashCode()); var binary = new BinaryChannel(socket); var message = new MessageChannel(binary); var device = new TcpDevice(brain, message, Clock.Default); connectedDevices.Add(device); device.Disconnected += (sender, e) => socket.Dispose(); socket.Disconnected += (sender, e) => { Console.WriteLine("Socket disconnected {0}", sender.GetHashCode()); connectedDevices.Remove(device); device.Dispose(); }; socket.Disposed += (sender, e) => { Console.WriteLine("Socket disposed {0}", sender.GetHashCode()); connectedDevices.Remove(device); device.Dispose(); }; }); server.Start(); Console.WriteLine("Define topic:"); Console.WriteLine(" topic [void|bool|number|string] [name]"); Console.WriteLine("Define device:"); Console.WriteLine(" device [type] [comma-separated list of topic commands the device can receive]"); Console.WriteLine("Define behavior:"); Console.WriteLine(" behave [when then expression]"); Console.WriteLine("Press Enter to exit"); string line = null; while ((line = Console.ReadLine()) != "") { if (line == Environment.NewLine) return; if (line.StartsWith("topic")) { var topic = TopicParser.Parse(line); topics[topic.Item1] = topic.Item2; Console.WriteLine("Registered topic '{0}' of type {1}", topic.Item1, topic.Item2); } else if (line.StartsWith("device")) { var device = DeviceParser.Parse(line); devices.Register(device.Item1, device.Item2.ToArray()); Console.WriteLine("Registered device type '{0}' to receive commands {1}", device.Item1, string.Join(", ", device.Item2.Select(s => "'" + s + "'"))); } else if (line.StartsWith("behave ")) { try { brain.Behave(line.Substring(7)); } catch (Exception e) { Console.WriteLine(e.Message); } } } } catch (Exception e) { Console.WriteLine("Failed: {0}", e); } }