You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Update table sorting API to allow for callback #616
Sorting an edge table is quite expensive, and the current implementation can be out-performed by client code. The main motivation here is to speed up forward simulations.
It has been proposed to allow a callback to be passed to tsk_table_collection_sort that would look something like:
The idea is that this call back does nothing, meaning that client code is assuming 100% responsibility here and accepting that the integrity checker will double-check their work. It turns out to be very fiddly to get complex callbacks in other languages to work when the calling conventions differ from C. (Read: my attempts to mock this up so far all segfault.). Thus, allowing the step to be just skipped internally would be useful.
My suggestion would be that we keep the existing API for tsk_table_collection_sort, but make the tsk_table_sorter part of the public API and put all the bells and whistles on that.
In terms of making hooks available, one thing we could do is have sort_edges be a function pointer which by default points to the existing implementation. Then, if you want to do something more fancy, you can use:
BTW, we'd change around the implementation of the table_sorter class a bit - the way things are done is left over from older code organisations, and it missed out a few refactorings. But the basic idea would stay the same.
Will this work with C++ ABI/calling convention issues @molpopgen?
Should. You can set the sorter to do nothing like I was thinking, and then run your own separately. The issue is that only a small subset of functional types in C++ convert to C function pointers without a lot of extra work to write trampolines.
In order for that to work, then your lamba expressions will need to do awful things like capture global variables. What is usually needed is the reverse of your example. The closure should have the signature of the simpler function, and itself called some more complex function that takes advantage of captured variables.
Will do. I'll try to post an example that actually compiles and does
things, which means I'll make a mock of the setup, or at least some minimal
abstraction of it. It'll take a bit of doing.
I think the void * tbd in @molpopgen's original proposal would suffice for SLiM. From that we could recover the simulation object, which would get us to the table collection, which would let us do the sort in parallel ourselves. BTW, it would be good for the comparator function to be public in tskit, if it isn't already, so that one would not have to hard-code the details of that in the client. (Although one might choose to do so anyway, for efficiency; but one shouldn't have to.)
The void • is normally provided to the callback, not by it. Often, it is
something about the internal state of the function calling the callback.
We need to do more than just satisfy slim here. We need it to work in
general, which is a bit harder.
Oh, sorry, I misunderstood the purposed to the void * tbd; I took that to be the fairly standard mechanism whereby one would pass in a void* to tskit when registering one's callback, and tskit would simply pass the void* back again when calling the callback, allowing any arbitrary pointer from the client to be provided back to the callback code. If that wasn't the intent, then I'd suggest doing that; it's a common design pattern that seems adequate for most purposes. And of course I wasn't suggesting that satisfying SLiM was the only important thing! Just saying "This design [as I understood it then] would satisfy SLiM, for what that's worth." :->
@bhaller -- if you want access to the pop object, then your callback becomes a closure, meaning that you bind (by one of the 3 or 4 possible mechanisms) your population, leaving a final signature of int(*)(table_collection *). You'd do that via a lambda, std::function, etc., and then it would crash on the C side. You'll see this in the example I write up.
If it helps, I can write in the comments of the example how function composition works? It'll take some time to write, so may not happen this week.
If it helps, I can write in the comments of the example how function composition works? It'll take some time to write, so may not happen this week.
No need; that is not what I was suggesting. I would simply pass in a SLiMSim* to tskit when registering my callback, tskit would hang on to that void* for me and pass it back to my callback, and my callback would be a C function that would cast the void* back to a SLiMSim* and use that to get the table collection and do the sort. Pure C, as far as tskit and the ABI are concerned (perhaps one would want to declare the callback function with extern "C", but in my experience that has never been necessary). And perfectly safe, as long as the table collection doesn't live longer than the SLiMSim object (in which case one would need to deregister the callback to get rid of the dangling pointer in the void*). What is the need to make it more complicated than this?
Let's get clarification from @jeromekelleher -- is the intent that the void * be an as-yet-undefined pointer to an internal state, or something which the client callback receives? If the latter, that changes how I'd write the example.
Assuming for the moment that I originally over-thought the problem, meaning @bhaller is correct and that the void * refers to user data, then we are good with an API that looks like this, which also allows for the callback to be undefined so that client code can do arbitrarily complicated things on their own.
The case I was originally envisioning, with tskit sending some internal state object to the callback, is a lot more complex to handle.
#include<vector>
#include<iostream>
#include<functional>// Mock some tskit stuffstructtsk_mock_table_collection
{
};
using tsk_mock_edge_sort_callback = int (*)(tsk_mock_table_collection *, void *);
structtsk_mock_table_sorter
{
tsk_mock_edge_sort_callback cback;
};
inttsk_mock_default_edge_sorting_fxn(tsk_mock_table_collection *tables, void *)
{
std::cout << "default callback\n";
return0;
}
inttsk_mock_table_sorter_init(tsk_mock_table_sorter *sorter)
{
sorter->cback = &tsk_mock_default_edge_sorting_fxn;
return0;
};
inttsk_mock_sort_tables(tsk_mock_table_sorter *sorter, tsk_mock_table_collection *tables,
void *client_data)
{
if (sorter->cback != nullptr)
{
sorter->cback(tables, client_data);
}
else
{
std::cout << "not sorting the tables, so hope you did your homework!\n";
}
return0;
}
// Mock some client stuffintvanilla_client_callback(tsk_mock_table_collection *, void *)
{
std::cout << "vanilla client callback\n";
return0;
}
struct_edge
{
// left, right, parent, child,// used for sorting
};
intcpp_callback_1(tsk_mock_table_collection *, void *)
{
std::cout << "cpp callback 1\n";
std::vector<_edge> edges;
// copy from tables to edges// sort via std::sort// copy backreturn0;
}
intcpp_callback_2(tsk_mock_table_collection *, void *data)
{
std::cout << "cpp callback 2\n";
auto edges = static_cast<std::vector<_edge> *>(data);
if (edges == nullptr)
{
return -1;
}
edges->clear(); // re-use those big allocations// copy/sort/copyreturn1;
}
// Typdef for a C++ callback via std::functionusing std_function_callback = std::function<int(tsk_mock_table_collection *, void *)>;
intmain(int argc, char **argv)
{
tsk_mock_table_sorter sorter;
tsk_mock_table_sorter_init(&sorter);
tsk_mock_table_collection tables;
tsk_mock_sort_tables(&sorter, &tables, nullptr);
// I promise that I know what I am doing
sorter.cback = nullptr;
tsk_mock_sort_tables(&sorter, &tables, nullptr);
// Now, explore more complex setups
sorter.cback = &vanilla_client_callback;
tsk_mock_sort_tables(&sorter, &tables, nullptr);
// A non-capturing lambda sufficesconstauto lambda_callback_0 = [](tsk_mock_table_collection *, void *) -> int {
std::cout << "lambda callback 0\n";
return0;
};
sorter.cback = lambda_callback_0;
tsk_mock_sort_tables(&sorter, &tables, nullptr);
constauto lambda_callback_1
= [](tsk_mock_table_collection *tables, void *tbd) -> int {
std::cout << "lambda callback 1 dispatches to";
returncpp_callback_1(tables, tbd);
};
sorter.cback = lambda_callback_1;
tsk_mock_sort_tables(&sorter, &tables, nullptr);
sorter.cback = &cpp_callback_2;
std::vector<_edge> edges;
tsk_mock_sort_tables(&sorter, &tables, &edges);
}
Activity
jeromekelleher commentedon May 15, 2020
The way it works at the moment is that table_collection_sort is a thin wrapper for the table_sorter private "class".
My suggestion would be that we keep the existing API for
tsk_table_collection_sort, but make thetsk_table_sorterpart of the public API and put all the bells and whistles on that.In terms of making hooks available, one thing we could do is have
sort_edgesbe a function pointer which by default points to the existing implementation. Then, if you want to do something more fancy, you can use:It's a bit more long-winded, but seems quite flexible and extensible.
Will this work with C++ ABI/calling convention issues @molpopgen?
jeromekelleher commentedon May 15, 2020
BTW, we'd change around the implementation of the
table_sorterclass a bit - the way things are done is left over from older code organisations, and it missed out a few refactorings. But the basic idea would stay the same.molpopgen commentedon May 15, 2020
Should. You can set the sorter to do nothing like I was thinking, and then run your own separately. The issue is that only a small subset of functional types in C++ convert to C function pointers without a lot of extra work to write trampolines.
jeromekelleher commentedon May 15, 2020
It should be possible wrap the C++ lambda fanciness with a simple function call though, right? So, you define your function
That should all just compile down to something simple enough then?
molpopgen commentedon May 15, 2020
In order for that to work, then your lamba expressions will need to do awful things like capture global variables. What is usually needed is the reverse of your example. The closure should have the signature of the simpler function, and itself called some more complex function that takes advantage of captured variables.
jeromekelleher commentedon May 15, 2020
Oh, OK, I was thinking too much like Python then. Would you mind pasting in an outline of what this would look like ideally from the C++ client?
molpopgen commentedon May 15, 2020
bhaller commentedon May 15, 2020
I think the
void * tbdin @molpopgen's original proposal would suffice for SLiM. From that we could recover the simulation object, which would get us to the table collection, which would let us do the sort in parallel ourselves. BTW, it would be good for the comparator function to be public in tskit, if it isn't already, so that one would not have to hard-code the details of that in the client. (Although one might choose to do so anyway, for efficiency; but one shouldn't have to.)molpopgen commentedon May 15, 2020
bhaller commentedon May 15, 2020
Oh, sorry, I misunderstood the purposed to the
void * tbd; I took that to be the fairly standard mechanism whereby one would pass in avoid*to tskit when registering one's callback, and tskit would simply pass thevoid*back again when calling the callback, allowing any arbitrary pointer from the client to be provided back to the callback code. If that wasn't the intent, then I'd suggest doing that; it's a common design pattern that seems adequate for most purposes. And of course I wasn't suggesting that satisfying SLiM was the only important thing! Just saying "This design [as I understood it then] would satisfy SLiM, for what that's worth." :->molpopgen commentedon May 15, 2020
@bhaller -- if you want access to the pop object, then your callback becomes a closure, meaning that you bind (by one of the 3 or 4 possible mechanisms) your population, leaving a final signature of
int(*)(table_collection *). You'd do that via a lambda,std::function, etc., and then it would crash on the C side. You'll see this in the example I write up.If it helps, I can write in the comments of the example how function composition works? It'll take some time to write, so may not happen this week.
bhaller commentedon May 15, 2020
No need; that is not what I was suggesting. I would simply pass in a
SLiMSim*to tskit when registering my callback, tskit would hang on to thatvoid*for me and pass it back to my callback, and my callback would be a C function that would cast thevoid*back to aSLiMSim*and use that to get the table collection and do the sort. Pure C, as far as tskit and the ABI are concerned (perhaps one would want to declare the callback function withextern "C", but in my experience that has never been necessary). And perfectly safe, as long as the table collection doesn't live longer than the SLiMSim object (in which case one would need to deregister the callback to get rid of the dangling pointer in the void*). What is the need to make it more complicated than this?molpopgen commentedon May 15, 2020
Let's get clarification from @jeromekelleher -- is the intent that the
void *be an as-yet-undefined pointer to an internal state, or something which the client callback receives? If the latter, that changes how I'd write the example.molpopgen commentedon May 15, 2020
Assuming for the moment that I originally over-thought the problem, meaning @bhaller is correct and that the
void *refers to user data, then we are good with an API that looks like this, which also allows for the callback to be undefined so that client code can do arbitrarily complicated things on their own.The case I was originally envisioning, with tskit sending some internal state object to the callback, is a lot more complex to handle.
molpopgen commentedon May 15, 2020
I never did anything using the
std::functioncase, btw.jeromekelleher commentedon May 18, 2020
Looks good to me @molpopgen, I'll try to get a PR with a proposal for the tskit API soon.
molpopgen commentedon May 18, 2020
Sounds good. I think I was being completely thick originally and had forgotten everything about C callbacks.
Expose sorting API and test C++ implementation.
Expose sorting API and test C++ implementation.
Expose sorting API and test C++ implementation.
Expose sorting API and test C++ implementation.
Expose sorting API and test C++ implementation.
Expose sorting API and test C++ implementation.
Expose sorting API and test C++ implementation.