The following code will have compile error at the following line: ray::Task(aggregate_results).Remote(futures);
How to correct the code?
#include <ray/api.h>
#include <iostream>
#include <vector>
#include <cmath>
using namespace ray;
double calculate_partial_integral(double a, double b, int steps) {
const double dx = (b - a) / steps;
double sum = 0.0;
for(int i = 0; i < steps; ++i) {
double x = a + (i + 0.5) * dx;
sum += 4.0 / (1.0 + x*x);
}
return sum * dx;
}
double aggregate_results(const std::vector<double>& partial_sums) {
return std::accumulate(partial_sums.begin(), partial_sums.end(), 0.0);
}
RAY_REMOTE(calculate_partial_integral, aggregate_results);
int main() {
ray::Init();
const double a = 0.0;
const double b = 1.0;
const int total_steps = 10000000;
const int num_tasks = 8;
const double interval = (b - a) / num_tasks;
const int steps_per_task = total_steps / num_tasks;
std::vector<ObjectRef<double>> futures;
for(int i = 0; i < num_tasks; ++i) {
double start = a + i * interval;
double end = start + interval;
auto future = ray::Task(calculate_partial_integral)
.Remote(start, end, steps_per_task);
futures.push_back(future);
}
auto result_future = ray::Task(aggregate_results).Remote(futures);
double pi_approx = *result_future.Get();
std::cout.precision(15);
std::cout << "computed value: " << pi_approx << "\n"
<< "real: " << M_PI << "\n"
<< "gap: " << std::abs(pi_approx - M_PI) << std::endl;
ray::Shutdown();
return 0;
}
Error message is like:
static_check.h:58:86: error: static assertion failed: arguments not match
58 | static_assert(is_invocable<Function, typename FilterArgType<Args>::type...>::value ||
| ~~~~~~^~
59 | is_invocable<Function, Args...>::value,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pi/static_check.h:58:86: note: '(((bool)std::integral_constant<bool, false>::value) || ((bool)std::integral_constant<bool, false>::value))' evaluates to false