Skip to content

Instantly share code, notes, and snippets.

@chathurawidanage
Created February 1, 2019 15:43
Show Gist options
  • Select an option

  • Save chathurawidanage/d89de1bcc2930f80208f23a3c2c6f6da to your computer and use it in GitHub Desktop.

Select an option

Save chathurawidanage/d89de1bcc2930f80208f23a3c2c6f6da to your computer and use it in GitHub Desktop.
Example showing how stucts can be used in HarpCPP
#include <iostream>
#include "harp.h"
using namespace std;
using namespace harp;
struct X {
double a;
double b;
};
class TestWorker : public Worker {
void execute(com::Communicator *comm, int argc, char *argv[]) {
cout << "Worker : " << this->workerId << " of " << this->worldSize << endl;
auto *a = new int[1 + workerId];
for (int i = 0; i < 1 + workerId; i++) {
a[i] = workerId;
}
auto yo = new X;
yo->a = workerId;
yo->b = workerId;
char *bytes = reinterpret_cast<char *>(yo);
harp::ds::Table<char> tab(0);
harp::ds::Partition<char> p(workerId, bytes, sizeof(X));
tab.addPartition(&p);
comm->allGather(&tab);
if (workerId == 0) {
util::print::printTable(&tab);
auto p = tab.getPartition(1);
char *data = p->getData();
X *deserializedX = reinterpret_cast<X *>(data);
std::cout << "Value from des" << deserializedX->a << std::endl;
}
//delete[]a;
comm->barrier();
}
};
int main(int argc, char *argv[]) {
TestWorker testWorker;
testWorker.init(argc, argv);
testWorker.start();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment