Skip to content

Commit

Permalink
Initial setup for integrate_and_allocate
Browse files Browse the repository at this point in the history
  • Loading branch information
kordejong committed Feb 19, 2024
1 parent a8d8c90 commit 7c518f0
Show file tree
Hide file tree
Showing 13 changed files with 802 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,7 @@ namespace lue {
[](hpx::future<Component>& component_f)
{
// component_f.wait(); // TODO
// TODO This assertion fails sometimes. Fix this!
lue_hpx_assert(component_f.is_ready());
return component_f.get();
});
Expand Down
18 changes: 15 additions & 3 deletions source/framework/image_land/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,30 @@ block()

foreach(Policies IN LISTS LUE_FRAMEWORK_IMAGE_LAND_OPERATION_POLICIES)
foreach(ZoneElement IN LISTS LUE_FRAMEWORK_ZONE_ELEMENTS)
foreach(IntegrandElement IN LISTS LUE_FRAMEWORK_FLOATING_POINT_ELEMENTS)
foreach(FloatingPointElement IN LISTS LUE_FRAMEWORK_FLOATING_POINT_ELEMENTS)
math(EXPR count "${count} + 1")

# Instantiate integrate (route)
# Instantiate integrate
set(output_pathname "${CMAKE_CURRENT_BINARY_DIR}/${offset}/integrate-${count}.cpp")
generate_template_instantiation(
INPUT_PATHNAME
"${CMAKE_CURRENT_SOURCE_DIR}/${offset}/integrate.cpp.in"
OUTPUT_PATHNAME
"${output_pathname}"
DICTIONARY
'{"Policies":"${Policies}","RouteID":"${ZoneElement}","IntegrandElement":"${IntegrandElement}","rank":"${rank}"}'
'{"Policies":"${Policies}","RouteID":"${ZoneElement}","IntegrandElement":"${FloatingPointElement}","rank":"${rank}"}'
)
list(APPEND generated_source_files "${output_pathname}")

# Instantiate integrate_and_allocate
set(output_pathname "${CMAKE_CURRENT_BINARY_DIR}/${offset}/integrate_and_allocate-${count}.cpp")
generate_template_instantiation(
INPUT_PATHNAME
"${CMAKE_CURRENT_SOURCE_DIR}/${offset}/integrate_and_allocate.cpp.in"
OUTPUT_PATHNAME
"${output_pathname}"
DICTIONARY
'{"Policies":"${Policies}","ZoneElement":"${ZoneElement}","FloatingPointElement":"${FloatingPointElement}","rank":"${rank}"}'
)
list(APPEND generated_source_files "${output_pathname}")
endforeach()
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include "lue/framework/algorithm/policy.hpp"
#include "lue/framework/partitioned_array.hpp"
#include "lue/framework/serial_route.hpp"
#include <functional>
#include <map>
#include <tuple>
#include <vector>


namespace lue {

template<typename Policies, Rank rank>
auto integrate_and_allocate(
Policies const& policies,
SerialRoute<policy::InputElementT<Policies, 0>, rank> const& route,
PartitionedArray<policy::InputElementT<Policies, 1>, rank> const& zone,
std::vector<
std::reference_wrapper<PartitionedArray<policy::InputElementT<Policies, 2>, rank> const>> const&
crop_fractions)
-> std::tuple<
// Crop fractions
std::vector<PartitionedArray<policy::OutputElementT<Policies, 0>, rank>>,
// Zonal production
std::map<policy::InputElementT<Policies, 1>, policy::OutputElementT<Policies, 1>>>;

} // namespace lue

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include "lue/framework/algorithm/integrate_and_allocate.hpp"


namespace lue {
namespace policy::integrate_and_allocate {

template<typename ZoneElement, typename FloatingPointElement>
using DefaultValuePolicies = policy::DefaultValuePolicies<
AllValuesWithinDomain<ZoneElement, ZoneElement, FloatingPointElement>,
OutputElements<FloatingPointElement, FloatingPointElement>,
InputElements<ZoneElement, ZoneElement, FloatingPointElement>>;

} // namespace policy::integrate_and_allocate


namespace value_policies {

template<typename ZoneElement, typename FloatingPointElement, Rank rank>
auto integrate_and_allocate(
SerialRoute<ZoneElement, rank> const& route,
PartitionedArray<ZoneElement, rank> const& zone,
std::vector<std::reference_wrapper<PartitionedArray<FloatingPointElement, rank> const>> const&
crop_fractions)
-> std::tuple<
std::vector<PartitionedArray<FloatingPointElement, rank>>,
std::map<ZoneElement, FloatingPointElement>>
{
using Policies =
policy::integrate_and_allocate::DefaultValuePolicies<ZoneElement, FloatingPointElement>;

return integrate_and_allocate(Policies{}, route, zone, crop_fractions);
}

} // namespace value_policies
} // namespace lue
2 changes: 2 additions & 0 deletions source/framework/image_land/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ endforeach()
# RUNTIME_OUTPUT_DIRECTORY and RUNTIME_OUTPUT_NAME.
add_library(lue_py_image_land SHARED
src/integrate.cpp
src/integrate_and_allocate.cpp
src/python_extension.cpp
src/submodule.cpp
)
Expand Down Expand Up @@ -52,6 +53,7 @@ target_link_libraries(lue_py_image_land
pybind11::thin_lto
pybind11::opt_size
pybind11::windows_extras
Python3::Python
)

# TODO pybind11_strip does not work on macOS, when building using Conda packages
Expand Down
50 changes: 50 additions & 0 deletions source/framework/image_land/python/src/integrate_and_allocate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "lue/framework/algorithm/value_policies/integrate_and_allocate.hpp"
// #include "lue/py/framework/core/partitioned_array.hpp"
// #include "lue/py/framework/core/vector_of_array.hpp"
#include <pybind11/stl.h>


namespace lue::image_land {
namespace detail {

template<typename ZoneElement, typename FloatingPointElement, Rank rank>
auto integrate_and_allocate(
SerialRoute<ZoneElement, rank> const& route,
PartitionedArray<ZoneElement, rank> const& zone,
std::vector<std::reference_wrapper<PartitionedArray<FloatingPointElement, rank> const>> const&
crop_fractions)
{
return lue::value_policies::integrate_and_allocate(route, zone, crop_fractions);
}


template<typename ZoneElement, typename FloatingPointElement, Rank rank>
void bind_integrate_and_allocate(pybind11::module& module)
{
module.def(
"integrate_and_allocate",
lue::image_land::detail::integrate_and_allocate<ZoneElement, FloatingPointElement, rank>,
pybind11::return_value_policy::move);
}

} // namespace detail


void bind_integrate_and_allocate(pybind11::module& module)
{
Rank const rank{2};

detail::bind_integrate_and_allocate<std::uint8_t, float, rank>(module);
detail::bind_integrate_and_allocate<std::uint32_t, float, rank>(module);
detail::bind_integrate_and_allocate<std::uint64_t, float, rank>(module);
detail::bind_integrate_and_allocate<std::int32_t, float, rank>(module);
detail::bind_integrate_and_allocate<std::int64_t, float, rank>(module);

detail::bind_integrate_and_allocate<std::uint8_t, double, rank>(module);
detail::bind_integrate_and_allocate<std::uint32_t, double, rank>(module);
detail::bind_integrate_and_allocate<std::uint64_t, double, rank>(module);
detail::bind_integrate_and_allocate<std::int32_t, double, rank>(module);
detail::bind_integrate_and_allocate<std::int64_t, double, rank>(module);
}

} // namespace lue::image_land
2 changes: 2 additions & 0 deletions source/framework/image_land/python/src/submodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace lue::image_land {

void bind_integrate(pybind11::module& module);
void bind_integrate_and_allocate(pybind11::module& module);


void init_submodule(pybind11::module& module)
Expand All @@ -18,6 +19,7 @@ namespace lue::image_land {
)");

bind_integrate(submodule);
bind_integrate_and_allocate(submodule);
}

} // namespace lue::image_land
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np

import lue.framework as lfr
import lue.image_land as img
import lue_test


def setUpModule():
lue_test.start_hpx_runtime()


def tearDownModule():
lue_test.stop_hpx_runtime()


class IntegrateAndAllocateTest(lue_test.TestCase):
@lue_test.framework_test_case
def test_overloads(self):
array_shape = (60, 40)
zone_fill_value = 1
suitability_fill_value = 5.5
max_nr_cells = 10
nr_crops = 3

for zone_dtype in [np.uint8, np.uint32, np.uint64, np.int32, np.int64]:
for floating_point_type in [np.float32, np.float64]:
zone = lfr.create_array(array_shape, zone_dtype, zone_fill_value)
suitability = lfr.create_array(
array_shape, floating_point_type, suitability_fill_value
)
route = lfr.decreasing_order(zone, suitability, max_nr_cells)

crop_fractions = [
lfr.create_array(array_shape, floating_point_type, zone_fill_value),
lfr.create_array(array_shape, floating_point_type, zone_fill_value),
lfr.create_array(array_shape, floating_point_type, zone_fill_value),
]

(
crop_fractions_we_got,
zonal_production_we_got,
) = img.integrate_and_allocate(route, zone, crop_fractions)

self.assertTrue(isinstance(crop_fractions_we_got, list))
# TODO self.assertEqual(len(crop_fractions_we_got), nr_crops)

self.assertTrue(isinstance(zonal_production_we_got, dict))
# TODO self.assertEqual(len(zonal_production_we_got), nr_crops)
11 changes: 11 additions & 0 deletions source/framework/image_land/src/integrate_and_allocate.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "lue/framework/algorithm/definition/integrate_and_allocate.hpp"
#include "lue/framework/algorithm/value_policies/integrate_and_allocate.hpp"


namespace lue {

LUE_INSTANTIATE_INTEGRATE_AND_ALLOCATE(
ESC(policy::integrate_and_allocate::{{ Policies }}<{{ ZoneElement }}, {{ FloatingPointElement }}>),
{{ rank }});

} // namespace lue
11 changes: 11 additions & 0 deletions source/framework/image_land/test/integrate_and_allocate_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#define BOOST_TEST_MODULE lue framework image_land integrate_and_allocate
#include "lue/framework/algorithm/value_policies/integrate_and_allocate.hpp"
#include "lue/framework/test/array.hpp"
#include "lue/framework/test/compare.hpp"
#include "lue/framework/test/hpx_unit_test.hpp"


BOOST_AUTO_TEST_CASE(use_case_01)
{
// TODO
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "lue/py/configure.hpp"
#include "lue/py/framework/algorithm/abs.hpp"
#include "lue/py/framework/algorithm/add.hpp"
#include "lue/py/framework/algorithm/divide.hpp"
Expand All @@ -16,7 +15,7 @@
#include "lue/py/framework/algorithm/pow.hpp"
#include "lue/py/framework/algorithm/subtract.hpp"
#include "lue/py/framework/stream.hpp"
#include <pybind11/stl.h>
#include <pybind11/pybind11.h>


namespace lue::framework {
Expand Down Expand Up @@ -51,7 +50,7 @@ namespace lue::framework {
auto class_ =
pybind11::class_<Array>(
module,
fmt::format("PartitionedArray_{}_{}", as_string<Element>(), rank).c_str(),
fmt::format("PartitionedArray<{}, {}>", as_string<Element>(), rank).c_str(),
fmt::format(
R"(
Partitioned array type for arrays of rank {}, containing array
Expand Down

0 comments on commit 7c518f0

Please sign in to comment.