Skip to content

Commit

Permalink
can now return tuples in macrotask
Browse files Browse the repository at this point in the history
  • Loading branch information
fbischoff committed Sep 14, 2024
1 parent 8b25b1f commit 602452b
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/madness/chem/mp3.cc
Original file line number Diff line number Diff line change
Expand Up @@ -978,10 +978,10 @@ double MP3::mp3_energy_contribution_macrotask_driver(const Pairs<CCPair>& mp2pai
taskq->print_taskq();
taskq->run_all();

double term_CD=cd_future->get();
double term_EF=ef_future->get();
double term_GHIJ=ghij_future->get();
double term_KLMN=klmn_future->get();
double term_CD=cd_future.get();
double term_EF=ef_future.get();
double term_GHIJ=ghij_future.get();
double term_KLMN=klmn_future.get();
double mp3_energy=term_CD+term_GHIJ+term_KLMN+term_EF;
if (world.rank()==0) {
printf("term_CD %12.8f\n",term_CD);
Expand Down
8 changes: 4 additions & 4 deletions src/madness/chem/mp3.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ class MP3 : public CCPotentials {
const Function<double,3>&,
const std::vector<std::string>& > argtupleT;

using resultT =std::shared_ptr<ScalarResult<double>>;
using resultT =ScalarResult<double>;

resultT allocator(World& world, const argtupleT& argtuple) const {
return std::shared_ptr<ScalarResult<double>>(new ScalarResult<double>(world));
return ScalarResult<double>(world);
}

resultT operator() (const std::string& diagram, // which diagram to calculate
Expand Down Expand Up @@ -131,8 +131,8 @@ class MP3 : public CCPotentials {
std::string msg = "Unknown MP3 diagram: " + diagram;
MADNESS_EXCEPTION(msg.c_str(), 1);
}
auto result1=std::shared_ptr<ScalarResult<double>>(new ScalarResult<double>(world));
*result1=result;
auto result1=ScalarResult<double>(world);
result1=result;
return result1;

};
Expand Down
4 changes: 4 additions & 0 deletions src/madness/mra/macrotaskq.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ class ScalarResult {
impl=newimpl;
}

uniqueidT id() const {
return impl->id();
}

/// accumulate, optional fence
void gaxpy(const double a, const T& right, double b, const bool fence=true) {
impl->gaxpy(a,right,b,fence);
Expand Down
13 changes: 7 additions & 6 deletions src/madness/mra/test_cloud.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,20 @@ int test_custom_worldobject(World& universe, World& subworld, Cloud& cloud) {
test_output t1("testing custom worldobject");
t1.set_cout_to_terminal();
cloud.set_debug(false);
auto o1 =std::shared_ptr<ScalarResult<>>(new ScalarResult(universe));
auto o5 =std::shared_ptr<ScalarResult<>>(new ScalarResult(universe));
auto o1 =std::shared_ptr<ScalarResultImpl<double>>(new ScalarResultImpl<double>(universe));
auto o5 =std::shared_ptr<ScalarResultImpl<double>>(new ScalarResultImpl<double>(universe));
*o1=1.2;
if (universe.rank() == 0) gaxpy(1.0,*o1,2.0,2.8);
// if (universe.rank() == 0) gaxpy(1.0,*o1,2.0,2.8);
if (universe.rank() == 0) o1->gaxpy(1.0,2.0,2.8);

auto adrecords = cloud.store(universe, o1);
MacroTaskQ::set_pmap(subworld);
print("world constructible",is_world_constructible<ScalarResult<>>::value);
print("world constructible",is_world_constructible<ScalarResultImpl<double>>::value);

cloud.set_force_load_from_cache(false);
auto o2 = cloud.load<std::shared_ptr<ScalarResult<>>>(subworld, adrecords);
auto o2 = cloud.load<std::shared_ptr<ScalarResultImpl<double>>>(subworld, adrecords);
cloud.set_force_load_from_cache(true);
auto o3 = cloud.load<std::shared_ptr<ScalarResult<>>>(subworld, adrecords);
auto o3 = cloud.load<std::shared_ptr<ScalarResultImpl<double>>>(subworld, adrecords);
double d1=o1->get_local();
double d2=o2->get_local();
double d3=o3->get_local();
Expand Down
111 changes: 106 additions & 5 deletions src/madness/mra/test_vectormacrotask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ class VectorOfScalarTask : public MacroTaskOperationBase{
const std::vector<real_function_3d>& f2) const {
World &world = f1[0].world();
auto result=scalar_result_vector<double>(world,f1.size());
for (int i=0; i<f1.size(); ++i) result[i]=double(i);
for (int i=0; i<f1.size(); ++i) result[i]=double(i+batch.input[0].begin);

return result;
}
};
Expand Down Expand Up @@ -212,6 +213,60 @@ class TupleTask : public MacroTaskOperationBase{
}
};

/// this task won't do anything, is mostly to check if the combinations compile
template<typename elementT, typename elementR>
class MixedTupleTask : public MacroTaskOperationBase{
public:
// you need to define the result type
// resultT must implement gaxpy(alpha, result, beta, contribution)
// with resultT result, contribution;
typedef std::tuple<elementT,elementR> resultT;

// you need to define the exact argument(s) of operator() as tuple
typedef std::tuple<const std::vector<real_function_3d> &> argtupleT;

// some allocators
template<typename T>
typename std::enable_if<std::is_same<T, Function<double,3>>::value, T>::type
allocator(World& world, std::size_t n) const {
return Function<double,3>(world);
}

template<typename T>
typename std::enable_if<std::is_same<T, std::vector<real_function_3d>>::value, T>::type
allocator(World& world, std::size_t n) const {
return zero_functions_compressed<double,3>(world,n);
}

template<typename T>
typename std::enable_if<std::is_same<T, std::vector<ScalarResult<double>>>::value, T>::type
allocator(World& world, std::size_t n) const {
return scalar_result_vector<double>(world,n);
}

template<typename T>
typename std::enable_if<std::is_same<T, ScalarResult<double>>::value, T>::type
allocator(World& world, std::size_t n) const {
return ScalarResult<double>(world);
}

resultT allocator(World &world, const argtupleT &argtuple) const {
std::size_t n=std::get<0>(argtuple).size();
auto v1=allocator<elementT>(world,n);
auto v2=allocator<elementR>(world,n);
return std::make_tuple(v1,v2);
}

resultT operator()(const std::vector<real_function_3d>& f1) const {
World &world = f1[0].world();
std::size_t n=f1.size();
auto v1=allocator<elementT>(world,n);
auto v2=allocator<elementR>(world,n);
return std::make_tuple(v1,v2);
}
};



int check_vector(World& universe, const std::vector<real_function_3d> &ref, const std::vector<real_function_3d> &test,
const std::string msg) {
Expand Down Expand Up @@ -330,14 +385,13 @@ int test_vector_of_scalar_task(World& universe, const std::vector<real_function_
if (universe.rank()==0) print("\nstarting VectorOfScalarTask\n");
VectorOfScalarTask t1;
std::vector<ScalarResult<double>> result = t1(v3, 2.0, v3);
for (auto& r : result) print("result",r.get());


MacroTask task1(universe, t1);
auto result2= task1(v3, 2.0, v3);
for (auto& r : result2) print("result",r.get());

int success=0;
double error=1.e-15;
for (int i=0; i<result.size(); ++i) error+=fabs(result[i].get()-result2[i].get());
int success = check(universe,1.e-15, error, "vector of scalar task");
return success;
}

Expand All @@ -361,6 +415,50 @@ int test_tuple_of_vectors(World& universe, const std::vector<real_function_3d>&
return success;
}


// this will only test compilation
int test_mixed_tuple(World& universe, const std::vector<real_function_3d>& v3) {
if (universe.rank()==0) print("\nstarting MixedTupleTask\n");

typedef real_function_3d type1;
typedef std::vector<real_function_3d> type2;
typedef ScalarResult<double> type3;
typedef std::vector<ScalarResult<double>> type4;
MixedTupleTask<type1,type1> t11;
MixedTupleTask<type1,type2> t12;
MixedTupleTask<type1,type3> t13;
MixedTupleTask<type1,type4> t14;
MixedTupleTask<type2,type1> t21;
MixedTupleTask<type2,type2> t22;
MixedTupleTask<type2,type3> t23;
MixedTupleTask<type2,type4> t24;
MixedTupleTask<type3,type1> t31;
MixedTupleTask<type3,type2> t32;
MixedTupleTask<type3,type3> t33;
MixedTupleTask<type3,type4> t34;
MixedTupleTask<type4,type1> t41;
MixedTupleTask<type4,type2> t42;
MixedTupleTask<type4,type3> t43;
MixedTupleTask<type4,type4> t44;
auto result11=t11(v3);
auto result12=t12(v3);
auto result13=t13(v3);
auto result14=t14(v3);
auto result21=t21(v3);
auto result22=t22(v3);
auto result23=t23(v3);
auto result24=t24(v3);
auto result31=t31(v3);
auto result32=t32(v3);
auto result33=t33(v3);
auto result34=t34(v3);
auto result41=t41(v3);
auto result42=t42(v3);
auto result43=t43(v3);
auto result44=t44(v3);
return 0;
}

int test_2d_partitioning(World& universe, const std::vector<real_function_3d>& v3) {
if (universe.rank() == 0) print("\nstarting 2d partitioning");
auto taskq = std::shared_ptr<MacroTaskQ>(new MacroTaskQ(universe, universe.size()));
Expand Down Expand Up @@ -417,6 +515,9 @@ int main(int argc, char **argv) {
success+=test_tuple_of_vectors(universe,v3);
timer1.tag("vector of tuples task execution");

success+=test_mixed_tuple(universe,v3);
timer1.tag("mixed tuple task execution");

success+=test_deferred(universe,v3,ref);
timer1.tag("deferred taskq execution");

Expand Down

0 comments on commit 602452b

Please sign in to comment.