views:

891

answers:

3

Hello there

I'm writing a parallel evolutionary algorithm library using C++, MPI and CUDA. I need to extract the raw data from my object oriented design and stick it into a flat array (or std::vector using stl-mpi) for it to be sent across to nodes or the cuda device.

The complete design is quite complex with a lot of inheritance to keep the library flexible. But the classes of interest are:

Genome class - contains vector of data. e.g. floats or bools.

Population class - contains vector of Genome objects and is initailised with a genome object.

First a genome object is created, then a reference to it is passed to population class constructor which creates its own vector of genomes based on passed one. (Hope that makes sense!)

So I thought I would add another member vector, say rawData to the population class. But the type of rawData would need to depend on the type of data stored in the genomes (or the original genome object).
Here lies the problem, as far as I am aware there is no way to dynamically set the type passed to template.

my pseudo-code would be

class genome {
    std::vector<bool> data;
}

template <class T>
class population {
    std::vector<genome> population;
    std::vector<T> rawData;
    void PackDataIntoRawData();
};

then when i create the population (which is actually a member object of another class), I would call:

genome myBitGenome();
population<type of myBitGenome.data> pop(myBitGenome);

Is there anyway to do this, or can anyone suggest another way to implement this.

Thanks in advance

A: 

I don't really understand the problem. Don't you just need to turn the "raw data" into a genome? Why do you need to store it? If you need special functions to deal with different kinds of raw data, you don't need templates, you can simply use function overloading.

anon
The reverse. My population contains a vector of genomes, which each contain data and a bunch of other methods.. I need to extract just the data from those genomes and put it into a normal vector (not a vector of objects, can't send objects with MPI). At the same time, I can't put in fixed member types to my population (e.g. vector<float> rawData) because the population class is used with many different types of genome
zenna
A: 

As far as dynamic typing is concerned, boost::variant is a pretty powerful tool.

But your problem seems to be fairly simple, so i'd recommend doing something like this :

template<typename DataPiece>
class Genome {
    typedef std::vector<DataPiece> Data;
    Data data;
}

template <class T_Genome>
class Population {
    std::vector<T_Genome> population;
    typedef typename T_Genome::Data Data;
    Data rawData;
    void PackDataIntoRawData();
};

And then use it :

typedef Genome<bool> MyGenome;
MyGenome myBItGenome;
Population<MyGenome> pop( myBitGenome );
Benoît
A: 

I don't understand why you need to store the rawData as a member either.

Have you looked at the boost serialization library?

You just provide serialization functions or methods for each of your classes. Methods for STL collections & primitives are already provided.

E.g.

template <class T_Genome>
class Population {
    template<class Archive>
    void serialize(Archive& ar, const unsigned int version)
    {
        using namespace boost::serialization;
        ar & BOOST_SERIALIZATION_NVP(population);
        // ...
    }

    // ...
};
jon hanson