Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

c++ - Instantiate function for all combinations of template params, choose instantation at run time

Sorry if this has been asked before, but I couldn't find this exact question.

I have a templated CUDA kernel that looks like this:

template<int firstTextureIndex, int secondTextureIndex, int thirdTextureIndex> __global__ void myKernel

The three texture index template types will range from 0-7 and will not be known until runtime. I need to instantiate all 512 combinations of this kernel and then call the correct template based on the runtime values of the texture indices.

I've never written any pre processing macros and am trying to avoid it. Another post, here, shows how to instantiate many class templates for one template variable recursively by doing this:

template<int i>
class loop {
    loop<i-1> x;
}

template<>
class loop<1> {
}

loop<10> l;

I'm struggling to extend that to 3 variables and a function (instead of a class) for my situation. Even if I figure out how to instantiate all of them that way, how do I actually call 1 out of 512 possibilities at runtime without nested switch statements? To illustrate, the nested switch statements I'm trying to avoid would be like:

switch(firstTextureIndex)
{
    case 0:
        switch(secondTextureIndex)
        {
            case 1:
                switch(thirdTextureIndex)
                {
                    case 2:
                        myKernel<0, 1, 2><<<grid, block>>>(param1, param2, param3);
                        break;
                }
             break;
        }
    break;
}

If I figure out how to instantiate 0-7 for all of them, could I call it like:

myKernel<i, j, k><<<grid, block>>>(param1, param2); 

if I make i, j, and k enum types containing only 0-7? This way the compiler could know all the possible values and since I instantiate them all it would be OK with it?

Please note that there are good reasons for this triple template to pass in texture indices, but I'm omitting the explanation for conciseness. Any help on instantiating and/or calling this kernel would be greatly appreciated.

Edit: Jarod42 provided a valid solution that does exactly what I asked. Unfortunately I now realize the c++ standard is important here. I'm using c++98/03 combined with the latest stable release of the boost library, so a solution using these would be ideal. I could potentially use c++11, but c++14 is out due to limitations of our compiler.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may do something like:

template <std::size_t I>
void do_job()
{
    myKernel<I / 64, (I / 8) % 8, I % 8>{}();
}

template <std::size_t ... Is>
void callMyKernel(std::index_sequence<Is...>, std::size_t i, std::size_t j, std::size_t k)
{
    std::function<void()> fs[] = {&do_job<Is>...};

    fs[i * 64 + j * 8 + k]();
}

void callMyKernel(std::size_t i, std::size_t j, std::size_t k)
{
    callMyKernel(std::make_index_sequence<512>{}, i, j, k);
}

Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...