C
C#8mo 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);
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];
});
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.)
2 Replies
mtreit
mtreit8mo ago
Use an interface? Oh you mentioned that. What's the issue exactly?
InsertPi
InsertPi8mo ago
i guess what type i need to accept in the function to be able to pass a class name and instantiate it, presupposing that it implements the interface sorry for the late response like my type signature would need to be
public void ParallelFor(int start, int end, ????? schedule = DotMP.Schedule.Static, /* etc... */)
public void ParallelFor(int start, int end, ????? schedule = DotMP.Schedule.Static, /* etc... */)
the "normal" solution would be to supply a generic argument but that doesn't mesh well with source compatibility (also how would I verify that said type implements IScheduler and instantiate an object of that type) @mtreit sorry for ping just wanted to bring your attention to this nevermind, i got it all figured out. it's a mess but it's source-compatible and performant