Skip to content

Commit

Permalink
support of sequential and MPI enabled version
Browse files Browse the repository at this point in the history
  • Loading branch information
michel2323 committed Apr 19, 2018
1 parent cd2c26d commit cefa8b7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
17 changes: 8 additions & 9 deletions include/ad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ original variable.
#include "perf.hpp"
#include "alg.hpp"
#include "user.hpp"
#include <mpi.h>
#include <stdlib.h>
#include "parallel.hpp"
#ifdef EIGEN
Expand Down Expand Up @@ -873,11 +872,11 @@ void propagateAD(pVector<double>& m0, pMatrix<double>& cv0, System& sys,
// exit(1);
// paduprop_gather(H);
// H.cutoff(0.7);
std::cout << "H nz: " << H.nz() << std::endl;
// std::cout << "H nz: " << H.nz() << std::endl;
H_tmp=H;
std::cout << "Cutrate: " << cutrate << std::endl;
// std::cout << "Cutrate: " << cutrate << std::endl;
// H_tmp.cutoff(cutrate);
std::cout << "H_tmp nz: " << H_tmp.nz() << std::endl;
// std::cout << "H_tmp nz: " << H_tmp.nz() << std::endl;
drivers.t3s_t2s_t1s_driver(m0, J, H_tmp, T, start, end);
drivers.t1s_driver(m0, J);
// T.zeros();
Expand Down Expand Up @@ -951,10 +950,10 @@ void propagateAD(pVector<double>& m0, pMatrix<double>& cv0, System& sys,

if (degree > 2) {
if(paduprop_getrank() == 0) {
std::cout << "Propagating covariance 3rd order part1" << std::endl;
std::cout << "NZ: " << cv0.nz() << std::endl;
std::cout << "Entries: " << dim*dim << std::endl;
std::cout << "Percent NZ: " << (cv0.nz()*100)/(dim*dim) << std::endl;
// std::cout << "Propagating covariance 3rd order part1" << std::endl;
// std::cout << "NZ: " << cv0.nz() << std::endl;
// std::cout << "Entries: " << dim*dim << std::endl;
// std::cout << "Percent NZ: " << (cv0.nz()*100)/(dim*dim) << std::endl;
}

double aux, kurt;
Expand All @@ -967,7 +966,7 @@ void propagateAD(pVector<double>& m0, pMatrix<double>& cv0, System& sys,

for (size_t pm = 0; pm < dim; ++pm) {
if(paduprop_getrank() == 0) {
std::cout << "pm: " << pm << std::endl;
// std::cout << "pm: " << pm << std::endl;
}
for (size_t pn = 0; pn < dim; ++pn) {
for (size_t i = 0; i < dim; ++i) {
Expand Down
4 changes: 3 additions & 1 deletion include/parallel.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifndef ADUPROP_PARALLEL_HPP_
#define ADUPROP_PARALLEL_HPP_

#ifdef MPI_VERSION
#include <mpi.h>
#endif
#include <fstream>
#include "alg.hpp"
#include <cstring>
using namespace alg;

int paduprop_init();
Expand Down
6 changes: 6 additions & 0 deletions include/perf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

#include <map>
#include <iostream>
#ifdef MPI_VERSION
#include <mpi.h>
#endif


// E.g. __rdtsc() or MPI_Wtime()
#ifdef MPI_VERSION
#define ADUPROP_TIMER MPI_Wtime()
#else
#define ADUPROP_TIMER __rdtsc()
#endif
// double for MPI_Wtime and uint64_t for __rdtsc()
#define ADUPROP_TIMER_TYPE double

Expand Down
66 changes: 51 additions & 15 deletions src/parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

std::ofstream paduprop_sink("/dev/null");
int paduprop_init() {
#ifdef MPI_VERSION
int ierr = MPI_Init(NULL, NULL);
#else
int ierr = 0;
#endif

if (paduprop_getrank() != 0) {
// Mute standard output
Expand All @@ -13,12 +17,15 @@ int paduprop_init() {
return ierr;
}
int paduprop_destroy() {
#ifdef MPI_VERSION
return MPI_Finalize();
#else
return 0;
#endif
}
size_t paduprop_getstart(size_t dim) {
int rank; int commsize;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &commsize);
int commsize = paduprop_getcommsize();
int rank = paduprop_getrank();
size_t chunk = dim/commsize;
size_t remain = dim%commsize;
size_t addleft = 0;
Expand All @@ -30,8 +37,7 @@ size_t paduprop_getstart(size_t dim) {
}

size_t paduprop_getstart(size_t dim, int rank) {
int commsize;
MPI_Comm_size(MPI_COMM_WORLD, &commsize);
int commsize = paduprop_getcommsize();
size_t chunk = dim/commsize;
size_t remain = dim%commsize;
size_t addleft = 0;
Expand All @@ -43,9 +49,8 @@ size_t paduprop_getstart(size_t dim, int rank) {
}

size_t paduprop_getend(size_t dim) {
int rank; int commsize;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &commsize);
int commsize = paduprop_getcommsize();
int rank = paduprop_getrank();
size_t chunk = dim/commsize;
size_t remain = dim%commsize;
size_t addright = 0;
Expand All @@ -57,8 +62,7 @@ size_t paduprop_getend(size_t dim) {
}

size_t paduprop_getend(size_t dim, int rank) {
int commsize;
MPI_Comm_size(MPI_COMM_WORLD, &commsize);
int commsize = paduprop_getcommsize();
size_t chunk = dim/commsize;
size_t remain = dim%commsize;
size_t addright = 0;
Expand All @@ -71,29 +75,46 @@ size_t paduprop_getend(size_t dim, int rank) {

int paduprop_getrank() {
int rank;
#ifdef MPI_VERSION
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#else
rank = 0;
#endif
return rank;
}
int paduprop_getcommsize() {
int size;
#ifdef MPI_VERSION
MPI_Comm_size(MPI_COMM_WORLD, &size);
#else
size = 1;
#endif
return size;
}
int paduprop_sum(pMatrix<double> &mat) {
size_t nrows = mat.nrows();
size_t ncols = mat.ncols();
size_t size = nrows*ncols;
double *ptr = mat.get_datap();
return MPI_Allreduce(MPI_IN_PLACE, ptr, size, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
int ierr = 0;
#ifdef MPI_VERSION
ierr = MPI_Allreduce(MPI_IN_PLACE, ptr, size, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
#endif
return ierr;
}

int paduprop_sum(pVector<double> &vec) {
size_t size = vec.dim();
double *ptr = vec.get_datap();
return MPI_Allreduce(MPI_IN_PLACE, ptr, size, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
int ierr = 0;
#ifdef MPI_VERSION
ierr = MPI_Allreduce(MPI_IN_PLACE, ptr, size, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
#endif
return ierr;
}

int paduprop_gather(pTensor3<double> &ten) {
int ierr = 0;
size_t dim = ten.get_d1();
size_t start = paduprop_getstart(dim);
size_t end = paduprop_getend(dim);
Expand All @@ -108,16 +129,22 @@ int paduprop_gather(pTensor3<double> &ten) {
recvcounts[i] = (paduprop_getend(dim,i) - paduprop_getstart(dim,i))*dim*dim;
count += recvcounts[i];
}
int ierr = MPI_Allgatherv(sendbuf, size, MPI_DOUBLE,
#ifdef MPI_VERSION
ierr = MPI_Allgatherv(sendbuf, size, MPI_DOUBLE,
&ten[0][0][0], recvcounts, displs, MPI_DOUBLE,
MPI_COMM_WORLD);
#else
std::memcpy (sendbuf, &ten[0][0][0], recvcounts[0] * sizeof(double));
#endif

delete [] recvcounts;
delete [] displs;
delete [] sendbuf;
return ierr;
}

int paduprop_gather(pMatrix<double> &mat) {
int ierr = 0;
size_t dim = mat.nrows();
size_t start = paduprop_getstart(dim);
size_t end = paduprop_getend(dim);
Expand All @@ -132,16 +159,21 @@ int paduprop_gather(pMatrix<double> &mat) {
recvcounts[i] = (paduprop_getend(dim,i) - paduprop_getstart(dim,i))*dim;
count += recvcounts[i];
}
int ierr = MPI_Allgatherv(sendbuf, size, MPI_DOUBLE,
#ifdef MPI_VERSION
ierr = MPI_Allgatherv(sendbuf, size, MPI_DOUBLE,
&mat[0][0], recvcounts, displs, MPI_DOUBLE,
MPI_COMM_WORLD);
#else
std::memcpy (sendbuf, &mat[0][0], recvcounts[0] * sizeof(double));
#endif
delete [] recvcounts;
delete [] displs;
delete [] sendbuf;
return ierr;
}

int paduprop_gather(pVector<double> &vec) {
int ierr = 0;
size_t dim = vec.dim();
size_t start = paduprop_getstart(dim);
size_t end = paduprop_getend(dim);
Expand All @@ -156,9 +188,13 @@ int paduprop_gather(pVector<double> &vec) {
recvcounts[i] = (paduprop_getend(dim,i) - paduprop_getstart(dim,i));
count += recvcounts[i];
}
int ierr = MPI_Allgatherv(sendbuf, size, MPI_DOUBLE,
#ifdef MPI_VERSION
ierr = MPI_Allgatherv(sendbuf, size, MPI_DOUBLE,
&vec[0], recvcounts, displs, MPI_DOUBLE,
MPI_COMM_WORLD);
#else
std::memcpy (sendbuf, &vec[0], recvcounts[0] * sizeof(double));
#endif
delete [] recvcounts;
delete [] displs;
delete [] sendbuf;
Expand Down

0 comments on commit cefa8b7

Please sign in to comment.