CanopyHydrology_kern1_multiple.cc 14.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//
// 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 "utils.hh"
#include "readers.hh"
#include "CanopyHydrology.hh"
#include "legion.h"
25
#include "domains.hh"
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

using namespace Legion;

namespace TaskIDs {
enum TaskIDs {
  TOP_LEVEL_TASK,
  INIT_PHENOLOGY,
  INIT_FORCING,
  CANOPY_HYDROLOGY_INTERCEPTION,
  UTIL_SUM_MIN_MAX_REDUCTION
};
} // namespace

namespace FieldIDs {
enum FieldIDs {
  ELAI,
  ESAI,
  FORC_AIR_TEMP,
  FORC_RAIN,
  FORC_SNOW,
  FORC_IRRIG,
  QFLX_PREC_INTR,
  QFLX_IRRIG,
  QFLX_PREC_GRND,
  QFLX_SNWCP_LIQ,
  QFLX_SNWCP_ICE,
  QFLX_SNOW_GRND_PATCH,
  QFLX_RAIN_GRND,
  H2O_CAN  
};
} // namespace


std::array<double,3> SumMinMaxReduction(const Task *task,
                 const std::vector<PhysicalRegion> &regions,
                 Context ctx, Runtime *runtime)
{
  assert(regions.size() == 1);
  assert(task->regions.size() == 1);
  assert(task->regions[0].privilege_fields.size() == 1);
66
  std::cout << "LOG: Executing SumMinMax Task" << std::endl;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  FieldID fid = *(task->regions[0].privilege_fields.begin());

  FieldAccessor<READ_ONLY,double,2,coord_t,
                Realm::AffineAccessor<double,2,coord_t> > field(regions[0], fid);
  Rect<2> rect = runtime->get_index_space_domain(ctx,
          task->regions[0].region.get_index_space());

  std::array<double,3> sum_min_max = {0., 0., 0.};
  for (PointInRectIterator<2> pir(rect); pir(); pir++) {  
    auto val = field[*pir];
    sum_min_max[0] += val;
    sum_min_max[1] = std::min(sum_min_max[1], val);
    sum_min_max[2] = std::max(sum_min_max[2], val);
  }
  return sum_min_max;
}
  


void InitPhenology(const Task *task,
                   const std::vector<PhysicalRegion> &regions,
                   Context ctx, Runtime *runtime)
{
  assert(regions.size() == 1);
  assert(task->regions.size() == 1);
  assert(task->regions[0].privilege_fields.size() == 2); // LAI, SAI

  std::cout << "LOG: Executing InitPhenology task" << std::endl;
  const FieldAccessor<WRITE_DISCARD,double,2> elai(regions[0], FieldIDs::ELAI);
  const FieldAccessor<WRITE_DISCARD,double,2> esai(regions[0], FieldIDs::ESAI);
97
98
99
100
101
102
103
104

  Rect<2> my_bounds = Domain(runtime->get_index_space_domain(regions[0].get_logical_region().get_index_space()));
  coord_t n_grid_cells = my_bounds.hi[0] - my_bounds.lo[0] + 1;
  coord_t n_pfts = my_bounds.hi[1] - my_bounds.lo[1] + 1;
  
  assert(n_grid_cells == 24); // hard coded as two reads of 2x 12 increments
  ELM::Utils::read_phenology("../links/surfacedataWBW.nc", 12, n_pfts, 0, elai, esai);
  ELM::Utils::read_phenology("../links/surfacedataBRW.nc", 12, n_pfts, 12, elai, esai);
105
106
107
108
109
110
111
112
113
114
115
}  

int InitForcing(const Task *task,
                 const std::vector<PhysicalRegion> &regions,
                 Context ctx, Runtime *runtime)
{
  assert(regions.size() == 1);
  assert(task->regions.size() == 1);
  assert(task->regions[0].privilege_fields.size() == 4); // rain, snow, temp, irrig

  std::cout << "LOG: Executing InitForcing task" << std::endl;
116
117
118
119
  Rect<2> my_bounds = Domain(runtime->get_index_space_domain(regions[0].get_logical_region().get_index_space()));
  coord_t n_times_max = my_bounds.hi[0] - my_bounds.lo[0] + 1;
  coord_t n_grid_cells = my_bounds.hi[1] - my_bounds.lo[1] + 1;
  
120
121
122
123
  // init rain, snow, and air temp through reader
  const FieldAccessor<WRITE_DISCARD,double,2> rain(regions[0], FieldIDs::FORC_RAIN);
  const FieldAccessor<WRITE_DISCARD,double,2> snow(regions[0], FieldIDs::FORC_SNOW);
  const FieldAccessor<WRITE_DISCARD,double,2> air_temp(regions[0], FieldIDs::FORC_AIR_TEMP);
124
  int n_times = ELM::Utils::read_forcing("../links/forcing", n_times_max, 0, n_grid_cells,
125
126
127
128
          rain, snow, air_temp);

  // init irrig to zero
  const FieldAccessor<WRITE_DISCARD,double,2> irrig(regions[0], FieldIDs::FORC_IRRIG);
129
  for (size_t t=0; t!=n_times_max; ++t) {
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    for (size_t g=0; g!=n_grid_cells; ++g) {
      irrig[t][g] = 0.;
    }
  }

  return n_times;
}  


void CanopyHydrology_Interception_task(const Task *task,
                    const std::vector<PhysicalRegion> &regions,
                    Context ctx, Runtime *runtime)
{
  assert(regions.size() == 3);
  assert(task->regions.size() == 3);
145
  std::cout << "LOG: Executing Interception task" << std::endl;
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

  // process args / parameters
  int lcv_time;
  double dtime, dewmx;
  int ltype, ctype, frac_veg_nosno;
  bool urbpoi, do_capsnow;
  using args_t = std::tuple<int, double, int, int, bool, bool, double, int>;
  std::tie(lcv_time, dtime, ltype, ctype, urbpoi, do_capsnow, dewmx, frac_veg_nosno) =
      *((args_t*) task->args);

  // get accessors
  using AffineAccessorRO = FieldAccessor<READ_ONLY,double,2,coord_t,
                                         Realm::AffineAccessor<double,2,coord_t> >;
  using AffineAccessorRW = FieldAccessor<READ_WRITE,double,2,coord_t,
                                         Realm::AffineAccessor<double,2,coord_t> >;
  
  // -- forcing
  const AffineAccessorRO forc_rain(regions[0], FieldIDs::FORC_RAIN);
  const AffineAccessorRO forc_snow(regions[0], FieldIDs::FORC_SNOW);
  const AffineAccessorRO forc_irrig(regions[0], FieldIDs::FORC_IRRIG);

  // -- phenology
  const AffineAccessorRO elai(regions[1], FieldIDs::ELAI);
  const AffineAccessorRO esai(regions[1], FieldIDs::ESAI);

  // -- output
  const AffineAccessorRW qflx_prec_intr(regions[2], FieldIDs::QFLX_PREC_INTR);
  const AffineAccessorRW qflx_irrig(regions[2], FieldIDs::QFLX_IRRIG);
  const AffineAccessorRW qflx_prec_grnd(regions[2], FieldIDs::QFLX_PREC_GRND);
  const AffineAccessorRW qflx_snwcp_liq(regions[2], FieldIDs::QFLX_SNWCP_LIQ);
  const AffineAccessorRW qflx_snwcp_ice(regions[2], FieldIDs::QFLX_SNWCP_ICE);
  const AffineAccessorRW qflx_snow_grnd_patch(regions[2], FieldIDs::QFLX_SNOW_GRND_PATCH);
  const AffineAccessorRW qflx_rain_grnd(regions[2], FieldIDs::QFLX_RAIN_GRND);
  const AffineAccessorRW h2ocan(regions[2], FieldIDs::H2O_CAN);

181
182
183
  LogicalRegion lr = regions[2].get_logical_region();
  IndexSpaceT<2> is(lr.get_index_space());
  Rect<2> bounds = Domain(runtime->get_index_space_domain(is));
184

185
186
  std::cout << "LOG: With bounds: " << bounds.lo << "," << bounds.hi << std::endl;
  
187
188
  int n_irrig_steps_left = 0.;  // NOTE: still not physical quite sure what to do with this one.
  
189
190
  for (size_t g = bounds.lo[0]; g != bounds.hi[0]+1; ++g) {
    for (size_t p = bounds.lo[1]; p != bounds.hi[1]+1; ++p) {
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
      ELM::CanopyHydrology_Interception(dtime,
              forc_rain[lcv_time][g], forc_snow[lcv_time][g], forc_irrig[lcv_time][g],
              ltype, ctype, urbpoi, do_capsnow,
              elai[g][p], esai[g][p], dewmx, frac_veg_nosno,
              h2ocan[g][p], n_irrig_steps_left,
              qflx_prec_intr[g][p], qflx_irrig[g][p], qflx_prec_grnd[g][p],
              qflx_snwcp_liq[g][p], qflx_snwcp_ice[g][p],
              qflx_snow_grnd_patch[g][p], qflx_rain_grnd[g][p]);
    }
  }
}
  
  

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;

211
212
213
214
215
216
  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;

217
218
219
220
  // -----------------------------------------------------------------------------
  // SETUP Phase
  // -----------------------------------------------------------------------------
  //
221
  // Create data
222
  //
223
224
225
226
  // grid cell x pft data for phenology
  auto phenology_fs_ids = std::vector<unsigned>{ FieldIDs::ELAI, FieldIDs::ESAI };
  Data2D phenology(n_grid_cells, n_pfts, n_parts, "phenology", phenology_fs_ids,
                   ctx, runtime);
227

228
229
  // ntimes x grid cells forcing data
  auto forcing_fs_ids = std::vector<unsigned>{
230
    FieldIDs::FORC_RAIN, FieldIDs::FORC_SNOW, FieldIDs::FORC_AIR_TEMP, FieldIDs::FORC_IRRIG};
231
232
233
234
235
  Data2D_Transposed forcing(n_grid_cells, n_times_max, n_parts, "forcing", forcing_fs_ids,
                            ctx, runtime);
  
  // grid cell x pft water state and flux outputs
  auto flux_fs_ids = std::vector<unsigned>{
236
237
238
239
    FieldIDs::QFLX_PREC_INTR, FieldIDs::QFLX_IRRIG, FieldIDs::QFLX_PREC_GRND,
    FieldIDs::QFLX_SNWCP_LIQ, FieldIDs::QFLX_SNWCP_ICE,
    FieldIDs::QFLX_SNOW_GRND_PATCH, FieldIDs::QFLX_RAIN_GRND,
    FieldIDs::H2O_CAN};
240
  Data2D flux(n_grid_cells, n_pfts, n_parts, "flux", flux_fs_ids, ctx, runtime);
241

242
243
244
  // 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));
245
246
247
248
249
250
251
252
  
  // -----------------------------------------------------------------------------
  // Initialization Phase
  // -----------------------------------------------------------------------------
  // launch task to read phenology
  std::cout << "LOG: Launching Init Phenology" << std::endl;
  TaskLauncher phenology_launcher(TaskIDs::INIT_PHENOLOGY, TaskArgument(NULL, 0));
  phenology_launcher.add_region_requirement(
253
254
      RegionRequirement(phenology.logical_region, WRITE_DISCARD, EXCLUSIVE,
                        phenology.logical_region));
255
256
257
258
259
260
261
  for (auto id : phenology_fs_ids) phenology_launcher.add_field(0,id);
  runtime->execute_task(ctx, phenology_launcher);

  // launch task to read forcing
  std::cout << "LOG: Launching Init Forcing" << std::endl;
  TaskLauncher forcing_launcher(TaskIDs::INIT_FORCING, TaskArgument(NULL, 0));
  forcing_launcher.add_region_requirement(
262
263
      RegionRequirement(forcing.logical_region, WRITE_DISCARD, EXCLUSIVE,
                        forcing.logical_region));
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
  for (auto id : forcing_fs_ids) forcing_launcher.add_field(0,id);
  auto forcing_future = runtime->execute_task(ctx, forcing_launcher);
  int n_times = forcing_future.get_result<int>();

  // launch task to call interception
  std::cout << "LOG: Launching Init Forcing" << std::endl;

  // -- fixed magic parameters as arguments
  const int ctype = 1;
  const int ltype = 1;
  const bool urbpoi = false;
  const bool do_capsnow = false;
  const int frac_veg_nosno = 1;
  const double dewmx = 0.1;
  const double dtime = 1800.0;

  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;

285
286
287
  // DEBUG HACK --ETC
  //n_times = 12;
  // END DEBUG HACK
288
289
290
  std::vector<Future> futures;
  for (int i=0; i!=n_times; ++i) {
    auto args = std::make_tuple(i, dtime, ltype, ctype, urbpoi, do_capsnow, dewmx, frac_veg_nosno);
291
292
293
294

    ArgumentMap arg_map;
    IndexLauncher interception_launcher(TaskIDs::CANOPY_HYDROLOGY_INTERCEPTION,
            color_space, TaskArgument(&args, sizeof(args)), arg_map);
295
296
297

    // -- permissions on forcing
    interception_launcher.add_region_requirement(
298
299
        RegionRequirement(forcing.logical_partition, forcing.projection_id,
                          READ_ONLY, EXCLUSIVE, forcing.logical_region));
300
301
302
303
304
305
    interception_launcher.add_field(0, FieldIDs::FORC_RAIN);
    interception_launcher.add_field(0, FieldIDs::FORC_SNOW);
    interception_launcher.add_field(0, FieldIDs::FORC_IRRIG);

    // -- permissions on phenology
    interception_launcher.add_region_requirement(
306
307
        RegionRequirement(phenology.logical_partition, phenology.projection_id,
                          READ_ONLY, EXCLUSIVE, phenology.logical_region));
308
309
310
311
312
    interception_launcher.add_field(1, FieldIDs::ELAI);
    interception_launcher.add_field(1, FieldIDs::ESAI);

    // -- permissions on output
    interception_launcher.add_region_requirement(
313
314
        RegionRequirement(flux.logical_partition, flux.projection_id,
                          READ_WRITE, EXCLUSIVE, flux.logical_region));
315
316
317
    for (auto id : flux_fs_ids) interception_launcher.add_field(2, id);

    // -- launch the interception
318
    runtime->execute_index_space(ctx, interception_launcher);
319
320

    // launch accumulator for h2ocan
321
    // NOTE: need to somehow make this a reduction or something?  Shouldn't be on the full region!
322
323
    TaskLauncher accumlate_launcher(TaskIDs::UTIL_SUM_MIN_MAX_REDUCTION, TaskArgument());
    accumlate_launcher.add_region_requirement(
324
        RegionRequirement(flux.logical_region, READ_ONLY, EXCLUSIVE, flux.logical_region));
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    accumlate_launcher.add_field(0, FieldIDs::H2O_CAN);
    futures.push_back(runtime->execute_task(ctx, accumlate_launcher));
  }

  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");
  }

  {
    TaskVariantRegistrar registrar(TaskIDs::INIT_PHENOLOGY, "initialize_phenology");
    registrar.add_constraint(ProcessorConstraint(Processor::LOC_PROC));
    registrar.set_leaf();
    Runtime::preregister_task_variant<InitPhenology>(registrar, "initialize_phenology");
  }

  {
    TaskVariantRegistrar registrar(TaskIDs::INIT_FORCING, "initialize_forcing");
    registrar.add_constraint(ProcessorConstraint(Processor::LOC_PROC));
    registrar.set_leaf();
    Runtime::preregister_task_variant<int,InitForcing>(registrar, "initialize_forcing");
  }


  {
    TaskVariantRegistrar registrar(TaskIDs::CANOPY_HYDROLOGY_INTERCEPTION, "CanopyHydrology_interception");
    registrar.add_constraint(ProcessorConstraint(Processor::LOC_PROC));
    registrar.set_leaf();
    Runtime::preregister_task_variant<CanopyHydrology_Interception_task>(registrar, "CanopyHydrology_interception");
  }

  {
    TaskVariantRegistrar registrar(TaskIDs::UTIL_SUM_MIN_MAX_REDUCTION, "sum_min_max_reduction");
    registrar.add_constraint(ProcessorConstraint(Processor::LOC_PROC));
    registrar.set_leaf();
    Runtime::preregister_task_variant<std::array<double,3>,SumMinMaxReduction>(registrar, "sum_min_max_reduction");
  }
382
383
384
385
386
387

  Runtime::preregister_projection_functor(Data2D::projection_id,
          new Data2D::LocalProjectionFunction());
  Runtime::preregister_projection_functor(Data2D_Transposed::projection_id,
          new Data2D_Transposed::LocalProjectionFunction());
  
388
389
390
391
392
393
394
395
396
  
  return Runtime::start(argc, argv);
}