Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VowpalWabbitThreadedLearning"/> class.
        /// </summary>
        /// <param name="settings">Common settings used for vw instances.</param>
        public VowpalWabbitThreadedLearning(VowpalWabbitSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (settings.ParallelOptions == null)
            {
                throw new ArgumentNullException("settings.ParallelOptions must be set");
            }
            Contract.EndContractBlock();

            this.Settings = settings;

            if (this.Settings.ParallelOptions.CancellationToken == null)
            {
                this.Settings.ParallelOptions.CancellationToken = new CancellationToken();
            }

            switch (this.Settings.ExampleDistribution)
            {
            case VowpalWabbitExampleDistribution.UniformRandom:
                this.exampleDistributor = _ => this.random.Next(this.observers.Length);
                break;

            case VowpalWabbitExampleDistribution.RoundRobin:
                this.exampleDistributor = localExampleCount => (int)(localExampleCount % this.observers.Length);
                break;
            }

            this.exampleCount = 0;
            this.syncActions  = new ConcurrentList <Action <VowpalWabbit> >();

            this.vws          = new VowpalWabbit[settings.ParallelOptions.MaxDegreeOfParallelism];
            this.actionBlocks = new ActionBlock <Action <VowpalWabbit> > [settings.ParallelOptions.MaxDegreeOfParallelism];
            this.observers    = new IObserver <Action <VowpalWabbit> > [settings.ParallelOptions.MaxDegreeOfParallelism];

            // setup AllReduce chain
            // root closure
            {
                var nodeSettings = (VowpalWabbitSettings)settings.Clone();
                nodeSettings.Node = 0;
                var vw = this.vws[0] = new VowpalWabbit(nodeSettings);

                var actionBlock = this.actionBlocks[0] = new ActionBlock <Action <VowpalWabbit> >(
                    action => action(vw),
                    new ExecutionDataflowBlockOptions
                {
                    MaxDegreeOfParallelism = 1,
                    TaskScheduler          = settings.ParallelOptions.TaskScheduler,
                    CancellationToken      = settings.ParallelOptions.CancellationToken,
                    BoundedCapacity        = (int)settings.MaxExampleQueueLengthPerInstance
                });
            }

            for (int i = 1; i < settings.ParallelOptions.MaxDegreeOfParallelism; i++)
            {
                // closure vars
                var nodeSettings = (VowpalWabbitSettings)settings.Clone();
                nodeSettings.Root = this.vws[0];
                nodeSettings.Node = (uint)i;
                var vw = this.vws[i] = new VowpalWabbit(nodeSettings);

                var actionBlock = this.actionBlocks[i] = new ActionBlock <Action <VowpalWabbit> >(
                    action => action(vw),
                    new ExecutionDataflowBlockOptions
                {
                    MaxDegreeOfParallelism = 1,
                    TaskScheduler          = settings.ParallelOptions.TaskScheduler,
                    CancellationToken      = settings.ParallelOptions.CancellationToken,
                    BoundedCapacity        = (int)settings.MaxExampleQueueLengthPerInstance
                });
            }

            // get observers to allow for blocking calls
            this.observers = this.actionBlocks.Select(ab => ab.AsObserver()).ToArray();

            this.completionTasks = new Task[settings.ParallelOptions.MaxDegreeOfParallelism];
            // root closure
            {
                var vw = this.vws[0];
                this.completionTasks[0] = this.actionBlocks[0].Completion
                                          .ContinueWith(_ =>
                {
                    // perform final AllReduce
                    vw.EndOfPass();

                    // execute synchronization actions
                    foreach (var syncAction in this.syncActions.RemoveAll())
                    {
                        syncAction(vw);
                    }
                });
            }

            for (int i = 1; i < this.vws.Length; i++)
            {
                // perform final AllReduce
                var vw = this.vws[i];
                this.completionTasks[i] = this.actionBlocks[i].Completion
                                          .ContinueWith(_ => vw.EndOfPass(), this.Settings.ParallelOptions.CancellationToken);
            }
        }
Ejemplo n.º 2
0
 internal VowpalWabbitExampleJsonValidator(VowpalWabbitSettings settings)
 {
     settings = (VowpalWabbitSettings)settings.Clone();
     settings.EnableStringExampleGeneration = true;
     this.vw = new VowpalWabbit(settings);
 }
 internal VowpalWabbitExampleJsonValidator(VowpalWabbitSettings settings)
 {
     settings = (VowpalWabbitSettings)settings.Clone();
     settings.EnableStringExampleGeneration = true;
     this.vw = new VowpalWabbit(settings);
 }