CanopyHydrology_kern1_multiple.cc 4.53 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// This example plays with geometric regions in Legion by figuring out one way
// to do geometric regions that are grid_cell x PFT.
//
// The first strategy is a 2D Rect IndexSpace
//

#include <array>
#include <sstream>
#include <iterator>
#include <exception>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <vector>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <fstream>

#include "legion.h"
22

23
#include "domains.hh"
Ethan Coon's avatar
Ethan Coon committed
24
#include "tasks.hh"
25
#include "CanopyHydrology.hh"
26
27
28
29
30
31
32
33
34

using namespace Legion;

void top_level_task(const Task *task,
                    const std::vector<PhysicalRegion> &regions,
                    Context ctx, Runtime *runtime)
{
  std::cout << "LOG: Executing Top Level Task" << std::endl;

35
36
37
38
39
40
  const int n_pfts = 17;
  const int n_times_max = 31 * 24 * 2; // max days per month times hours per
                                       // day * half hour timestep
  const int n_grid_cells = 24;
  const int n_parts = 4;

41
42
43
44
  // -----------------------------------------------------------------------------
  // SETUP Phase
  // -----------------------------------------------------------------------------
  //
45
  // Create data
46
  //
47
  // grid cell x pft data for phenology
Ethan Coon's avatar
Ethan Coon committed
48
49
  auto phenology_fs_names = std::vector<std::string>{ "elai", "esai" };
  Data2D phenology(n_grid_cells, n_pfts, n_parts, "phenology", phenology_fs_names,
50
                   ctx, runtime);
Ethan Coon's avatar
Ethan Coon committed
51
52
53
54
55
56
  
  // n_times_max x n_grid_cells forcing data
  auto forcing_fs_ids = std::vector<std::string>{
    "forc_rain", "forc_snow", "forc_air_temp", "forc_irrig"};
  Data2D_Transposed forcing(n_grid_cells, n_times_max, n_parts, "forcing",
                            forcing_fs_ids, ctx, runtime);
57
58
  
  // grid cell x pft water state and flux outputs
Ethan Coon's avatar
Ethan Coon committed
59
60
61
  auto flux_fs_ids = std::vector<std::string>{
    "qflx_prec_intr", "qflx_irrig", "qflx_prec_grnd", "qflx_snwcp_liq",
    "qflx_snwcp_ice", "qflx_snow_grnd_patch", "qflx_rain_grnd", "h2ocan"};
62
  Data2D flux(n_grid_cells, n_pfts, n_parts, "flux", flux_fs_ids, ctx, runtime);
63
64
65
66
67

  // -----------------------------------------------------------------------------
  // Initialization Phase
  // -----------------------------------------------------------------------------
  // launch task to read phenology
68
  InitPhenology().launch(ctx, runtime, phenology);
69
70
71

  // launch task to read forcing
  std::cout << "LOG: Launching Init Forcing" << std::endl;
72
73
74
75
76
77
  auto forc_future = InitForcing().launch(ctx, runtime, forcing);
  int n_times = forc_future.get_result<int>();
  
  // -----------------------------------------------------------------------------
  // Run Phase
  // -----------------------------------------------------------------------------
78
79
80
81
82
  std::ofstream soln_file;
  soln_file.open("test_CanopyHydrology_kern1_multiple.soln");
  soln_file << "Time\t Total Canopy Water\t Min Water\t Max Water" << std::endl;
  soln_file << std::setprecision(16) << 0 << "\t" << 0.0 << "\t" << 0.0 << "\t" << 0.0 << std::endl;

83
84
85
86
  // create a color space for indexed launching.  This is what a Data1D
  // color_space would look like.
  auto color_space = Rect<1>(Point<1>(0), Point<1>(n_parts-1));
  
87
88
  std::vector<Future> futures;
  for (int i=0; i!=n_times; ++i) {
89
90
91
92
    // launch interception
    CanopyHydrology_Interception().launch(ctx, runtime, color_space,
            phenology, forcing, flux, i);
    
93
    // launch accumulator for h2ocan
94
    futures.push_back(SumMinMaxReduction().launch(ctx, runtime, flux, "h2ocan"));
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  }

  int i = 0;
  for (auto future : futures) {
    i++;
    //
    // write out to file
    //  
    auto sum_min_max = future.get_result<std::array<double,3>>();
    soln_file << std::setprecision(16) << i << "\t" << sum_min_max[0]
              << "\t" << sum_min_max[1]
              << "\t" << sum_min_max[2] << std::endl;
  }
}


// Main just calls top level task
int main(int argc, char **argv)
{
  Runtime::set_top_level_task_id(TaskIDs::TOP_LEVEL_TASK);

  {
    TaskVariantRegistrar registrar(TaskIDs::TOP_LEVEL_TASK, "top_level");
    registrar.add_constraint(ProcessorConstraint(Processor::LOC_PROC));
    Runtime::preregister_task_variant<top_level_task>(registrar, "top_level");
  }

122
123
124
  CanopyHydrology_Interception::preregister();
  InitForcing::preregister();
  InitPhenology::preregister();
Ethan Coon's avatar
Ethan Coon committed
125
  SumMinMaxReduction::preregister();
126
127
128
129
130
131

  Runtime::preregister_projection_functor(Data2D::projection_id,
          new Data2D::LocalProjectionFunction());
  Runtime::preregister_projection_functor(Data2D_Transposed::projection_id,
          new Data2D_Transposed::LocalProjectionFunction());
  
132
133
134
135
136
137
138
139
140
  
  return Runtime::start(argc, argv);
}