C#C
C#3y ago
InsertPi

✅ Replace enum with class name

I'm working on an API right now which, for those familiar with OpenMP, is a parallel programming API which aims to implement OpenMP functionality as faithfully as possible.

With that said, the current API has the following syntax for declaring a parallel-for loop:
DotMP.Parallel.ParallelFor(
    int start,
    int end,
    DotMP.Schedule schedule = DotMP.Schedule.Static, //enum
    uint? chunk_size = null, //defined by runtime if unset
    uint? num_threads = null, //defined by runtime if unset
    Action<int> action);

I am looking at the possibility of implementing custom schedulers as defined by the user through some sort of consistent interface. I am not looking for API/ABI compatibility, but source code compatibility would be highly desired. What I would like is to be able to have the DotMP.Schedule enum be replaced with classes that implement the IScheduler interface. How would I incorporate this into the type signature to be source-compatible? For instance:
DotMP.Parallel.ParallelFor(0, N, schedule: DotMP.Schedule.Dynamic /* this is a class name */, action: i =>
{
    y[i] += a * x[i];
});


(Before you ask: yes, I know about the TPL. Yes, I have my reasons for not using it. No, I don't want to get into them in this thread, I want to focus on the issue at hand. The provided function is extremely similar to System.Threading.Tasks.Parallel.For on the surface, but it is implemented significantly different, and there's much more to my library besides this that the TPL is not flexible enough to do.)
Was this page helpful?