Esempio n. 1
0
 // check for unsupported options
 internal static void CheckOptions(OTasks.ParallelOptions options)
 {
     if (options.TaskScheduler != (new OTasks.ParallelOptions().TaskScheduler))
     {
         throw new NotImplementedException("Do not support custom task schedulers");
     }
 }
Esempio n. 2
0
 public static OTasks.ParallelLoopResult For(int from, int to, OTasks.ParallelOptions options, Action <int, OTasks.ParallelLoopState> body)
 {
     return(Helper.SimpleWrap <OTasks.ParallelLoopResult>(
                (ClrSyncManager m) =>
     {
         throw new NotImplementedException("This form of Parallel.For is not yet supported by CHESS.");
     },
                () =>
     {
         return For(from, to, options, body);
     }
                ));
 }
Esempio n. 3
0
 public static OTasks.ParallelLoopResult For <TLocal>(long from, long to, OTasks.ParallelOptions options, Func <TLocal> init, Func <long, OTasks.ParallelLoopState, TLocal, TLocal> body, Action <TLocal> final)
 {
     return(Helper.SimpleWrap <OTasks.ParallelLoopResult>(
                (ClrSyncManager m) =>
     {
         throw new NotImplementedException("This form of Parallel.For is not yet supported by CHESS.");
     },
                () =>
     {
         return For(from, to, options, init, body, final);
     }
                ));
 }
Esempio n. 4
0
        public static OTasks.ParallelLoopResult For(int from, int to, OTasks.ParallelOptions options, Action <int> body)
        {
            bool singlethread = options.MaxDegreeOfParallelism == 1;

            Parallel.CheckOptions(options);
            return(ParallelForHelper(
                       from, to, singlethread, from, to, "Parallel.For",
                       (long i) => body((int)i),
                       delegate()
            {
                return OTasks.Parallel.For(from, to, options, body);
            }
                       ).Value);
        }
Esempio n. 5
0
        public static void Invoke(OTasks.ParallelOptions options, params Action[] actions)
        {
            int  minsplit     = 0;
            int  maxsplit     = actions.Length;
            bool singlethread = options.MaxDegreeOfParallelism == 1;

            CheckOptions(options);

            // accommodate expectation of true concurrency for small numbers of delegates
            if (!singlethread && actions.Length > 1 && actions.Length < 5)
            {
                minsplit++;
                maxsplit--;
            }

            ParallelForHelper(
                0, actions.Length, singlethread, minsplit, maxsplit,
                "Parallel.Invoke",
                (long i) => actions[i](),
                () => { OTasks.Parallel.Invoke(options, actions); return(null); }
                );
        }
Esempio n. 6
0
 public static OTasks.ParallelLoopResult ForEach <TSource, TLocal>(global::System.Collections.Concurrent.Partitioner <TSource> source, OTasks.ParallelOptions parallelOptions, Func <TLocal> localInit, Func <TSource, OTasks.ParallelLoopState, TLocal, TLocal> body, Action <TLocal> localFinally)
 {
     throw new NotImplementedException("The Parallel.ForEach<TSource, TLocal>(...) overloads are not instrumented by Chess.");
 }
Esempio n. 7
0
 public static OTasks.ParallelLoopResult ForEach <TSource, TLocal>(IEnumerable <TSource> source, OTasks.ParallelOptions parallelOptions, Func <TLocal> localInit, Func <TSource, OTasks.ParallelLoopState, TLocal, TLocal> body, Action <TLocal> localFinally)
 {
     throw new NotImplementedException("The Parallel.ForEach<TSource, TLocal>(...) overloads are not instrumented by Chess.");
 }
Esempio n. 8
0
 public static OTasks.ParallelLoopResult ForEach <TSource>(global::System.Collections.Concurrent.Partitioner <TSource> part, OTasks.ParallelOptions options, Action <TSource> body)
 {
     return(Helper.SimpleWrap <OTasks.ParallelLoopResult>(
                (ClrSyncManager m) => {
         throw new NotImplementedException("This form of Parallel.ForEach is not yet supported by CHESS.");
     },
                () => {
         return OTasks.Parallel.ForEach <TSource>(part, options, body);
     }
                ));
 }
Esempio n. 9
0
        public static OTasks.ParallelLoopResult ForEach <TSource>(IEnumerable <TSource> source, OTasks.ParallelOptions options, Action <TSource> body)
        {
            List <TSource> elts         = new List <TSource>();
            bool           singlethread = options.MaxDegreeOfParallelism == 1;

            Parallel.CheckOptions(options);

            return(ParallelForHelper(
                       () => {
                // this is called outside of chess
                IEnumerator <TSource> enumerator = source.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    elts.Add(enumerator.Current);
                }
                return new Range(0, elts.Count, 0, elts.Count);
            },
                       singlethread,
                       "Parallel.ForEach",
                       (long i) => body(elts[(int)i]),
                       delegate() {
                return OTasks.Parallel.ForEach <TSource>(source, body);
            }
                       ).Value);
        }
Esempio n. 10
0
 public static OTasks.ParallelLoopResult ForEach <TSource>(IEnumerable <TSource> source, OTasks.ParallelOptions options, Action <TSource, OTasks.ParallelLoopState> body)
 {
     return(Helper.SimpleWrap <OTasks.ParallelLoopResult>(
                (ClrSyncManager m) => {
         throw new NotImplementedException("This form of Parallel.ForEach is not yet supported by CHESS.");
     },
                () => {
         return OTasks.Parallel.ForEach <TSource>(source, options, body);
     }
                ));
 }
Esempio n. 11
0
        // variable-range parallel for loop helper
        internal static OTasks.ParallelLoopResult?ParallelForHelper(
            Func <Range> prologue,
            bool singlethread,
            string name,
            global::System.Action <long> body,
            Helper.SimpleDel <OTasks.ParallelLoopResult?> orig)
        {
            return
                (Helper.SimpleContextExposingWrap(
                     delegate(ClrSyncManager manager, ref IDisposable protectingcontext)
            {
                Range range = new Range();

                // do prologue outside chess... and switch protecting context
                MChessChess.LeaveChess();
                protectingcontext.Dispose();
                Exception ex = null;
                try
                {
                    range = prologue();
                }
                catch (Exception e)          // catch recoverable exception in user code
                {
                    ex = e;
                }
                IDisposable pc = _ProtectingThreadContext.Acquire();
                MChessChess.EnterChess();

                if (ex != null)
                {
                    global::System.Diagnostics.Debug.Fail("Not implemented: exceptions in enumerators of Parallel foreach");
                    throw ex;
                }

                if (range.to <= range.from)          // empty loop
                {
                    return orig();
                }

                long split;
                if (!singlethread)
                {
                    split = range.minsplit + manager.Choose((int)(range.maxsplit - range.minsplit + 1));
                }
                else
                {
                    split = (manager.Choose(2) == 0) ? range.minsplit : range.maxsplit;
                }

                MChessChess.TraceEvent(name + "(" + range.from + "," + range.to + "), split " + split);

                SimplePartitioner <long> partitioner = new SimplePartitioner <long>(
                    manager, range.from, range.to, split,
                    (long i) => { return i; },
                    true);

                // store protecting context
                partitioner.protectingcontext = pc;

                // TODO better job here
                OTasks.ParallelOptions options = new OTasks.ParallelOptions();
                options.MaxDegreeOfParallelism = 2;

                OTasks.ParallelLoopResult result = OTasks.Parallel.ForEach <long>(partitioner, options, partitioner.WrapBody(body));

                // switch protecting context back
                partitioner.protectingcontext.Dispose();
                protectingcontext = _ProtectingThreadContext.Acquire();

                return result;
            },
                     orig
                     ));
        }