mardi 21 novembre 2023

template specialization of unique_ptr generating member function

I am trying to make comm class with template variable. My colleague ask me to use std::unique_ptr for memory management. But I think I failed to implement polymorphysm.

I wrote code as the following.

template<typename env>
class Comm{
public:
    Comm(size_t rank, size_t world_size): rank(rank), world_size(world_size) {};
    Comm(){};
    ~Comm(){};
    int rank;
    int world_size;
}
template<typename env>
std::unique_ptr<Comm<env> > createComm(int argc, char *argv[]){
    std::cout << "empty comm" << std::endl;
    return std::make_unique< Comm<env> >();
};
template<>
std::unique_ptr<Comm<MKL> > createComm(int argc, char *argv[]){
    std::cout << "SERIALcomm" << std::endl;
    return std::make_unique< Comm<MKL> >( 0, 1 );
}
template<>
std::unique_ptr<Comm<MPI> > createComm(int argc, char *argv[]){
    std::cout << "MPIcomm" << std::endl;
    MPI_Init(&argc, &argv);
    int myRank ,nRanks;
    MPI_Comm_rank(mpi_comm, &myRank);
    MPI_Comm_size(mpi_comm, &nRanks);
    assert(nRanks>0);
    assert(myRank>=0);
    return std::make_unique< Comm<MPI> >( (size_t) myRank, (size_t) nRanks );
}

And here is a test code

int main(int argc, char* argv[]){
    auto comm = createComm<MKL>(argc, argv);
    return 0;
}

and I've got "empty comm" as a output.

What should I do to make MKL type or MPI comm object using createComm method?

Aucun commentaire:

Enregistrer un commentaire