Skip to content

Instantly share code, notes, and snippets.

@Szpadel
Last active August 29, 2015 13:57
Show Gist options
  • Save Szpadel/9839021 to your computer and use it in GitHub Desktop.
Save Szpadel/9839021 to your computer and use it in GitHub Desktop.
MPI Datatybe Builder
#include <mpidatatypebuilder.h>
#include <mpi.h>
using namespace std;
struct DataSet {
double vectorA[131072];
double vectorB[131072];
double arg1;
double arg2;
};
int main(int argc, char** argv)
{
// ...
DataSet emptySet;
MpiDatatypeBuilder builder(&emptySet, 4);
builder
.addVariable(&emptySet.vectorA, MPI_DOUBLE, 131072)
->addVariable(&emptySet.vectorB, MPI_DOUBLE, 131072)
->addVariable(&emptySet.arg1, MPI_DOUBLE, 1)
->addVariable(&emptySet.arg2, MPI_DOUBLE, 1);
MPI_Datatype dataSetDatatype = builder.build();
// ...
return 0;
}
#include "mpidatatypebuilder.h"
MpiDatatypeBuilder::MpiDatatypeBuilder(void *structure, int varCount)
{
MPI_Get_address(structure, &this->base);
this->dataSizes = new int[varCount];
this->dataTypes = new MPI_Datatype[varCount];
this->offsets = new MPI_Aint[varCount];
this->curVar = 0;
}
MpiDatatypeBuilder::~MpiDatatypeBuilder()
{
delete this->offsets;
delete this->dataTypes;
delete this->dataSizes;
}
MpiDatatypeBuilder* MpiDatatypeBuilder::addVariable(void *variablePtr, MPI_Datatype datatype, int count) {
MPI_Get_address(variablePtr, &this->offsets[this->curVar]);
this->offsets[this->curVar] -= this->base;
this->dataTypes[this->curVar] = datatype;
this->dataSizes[this->curVar] = count;
this->curVar++;
return this;
}
MPI_Datatype MpiDatatypeBuilder::build()
{
MPI_Datatype datatype;
MPI_Aint lb, range;
MPI_Type_struct(this->curVar, this->dataSizes, this->offsets, this->dataTypes, &datatype);
MPI_Type_get_extent(datatype, &lb, &range);
MPI_Type_create_resized(datatype, lb, range, &datatype);
MPI_Type_commit(&datatype);
return datatype;
}
#ifndef MPIDATATYPEBUILDER_H
#define MPIDATATYPEBUILDER_H
#include <mpi.h>
/**
* @brief The MpiDatatypeBuilder class
* @Author Piotr Rogowski <piotrekrogowski@gmail.com>
* @Licence CC BY-NC-SA 4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/
*/
class MpiDatatypeBuilder
{
int *dataSizes;
MPI_Datatype *dataTypes;
MPI_Aint *offsets;
MPI_Aint base;
int curVar;
public:
MpiDatatypeBuilder(void* structure, int varCount);
~MpiDatatypeBuilder();
MpiDatatypeBuilder* addVariable(void* variablePtr, MPI_Datatype datatype, int count);
MPI_Datatype build();
};
#endif // MPIDATATYPEBUILDER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment