File: TestOffsetView.hpp

package info (click to toggle)
kokkos 4.7.01-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,636 kB
  • sloc: cpp: 223,676; sh: 2,446; makefile: 2,437; python: 91; fortran: 4; ansic: 2
file content (779 lines) | stat: -rw-r--r-- 26,423 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
66
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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//@HEADER
// ************************************************************************
//
//                        Kokkos v. 4.0
//       Copyright (2022) National Technology & Engineering
//               Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER

/*
 * FIXME the OffsetView class is really not very well tested.
 */
#ifndef CONTAINERS_UNIT_TESTS_TESTOFFSETVIEW_HPP_
#define CONTAINERS_UNIT_TESTS_TESTOFFSETVIEW_HPP_

#include <gtest/gtest.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <Kokkos_Timer.hpp>
#include <Kokkos_OffsetView.hpp>

using std::cout;
using std::endl;

namespace Test {

template <typename Scalar, typename Device>
void test_offsetview_construction() {
  using offset_view_type = Kokkos::Experimental::OffsetView<Scalar**, Device>;
  using view_type        = Kokkos::View<Scalar**, Device>;

  std::pair<int64_t, int64_t> range0 = {-1, 3};
  std::pair<int64_t, int64_t> range1 = {-2, 2};

  {
    offset_view_type o1;
    ASSERT_FALSE(o1.is_allocated());

    o1 = offset_view_type("o1", {-1, 3}, {-2, 2});
    offset_view_type o2(o1);
    offset_view_type o3("o3", range0, range1);

    ASSERT_TRUE(o1.is_allocated());
    ASSERT_TRUE(o2.is_allocated());
    ASSERT_TRUE(o3.is_allocated());
  }

  offset_view_type ov("firstOV", range0, range1);

  ASSERT_EQ("firstOV", ov.label());

#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
  KOKKOS_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH()
#endif
#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
  ASSERT_EQ(2u, ov.Rank);
#endif
#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
  KOKKOS_IMPL_DISABLE_DEPRECATED_WARNINGS_POP()
#endif

  ASSERT_EQ(2u, ov.rank());

  ASSERT_EQ(ov.begin(0), -1);
  ASSERT_EQ(ov.end(0), 4);

  ASSERT_EQ(ov.begin(1), -2);
  ASSERT_EQ(ov.end(1), 3);

  ASSERT_EQ(ov.extent(0), 5u);
  ASSERT_EQ(ov.extent(1), 5u);

  {
    Kokkos::Experimental::OffsetView<Scalar*, Device> offsetV1("OneDOffsetView",
                                                               range0);

    Kokkos::RangePolicy<Device, int> rangePolicy1(offsetV1.begin(0),
                                                  offsetV1.end(0));
    Kokkos::parallel_for(
        rangePolicy1, KOKKOS_LAMBDA(const int i) { offsetV1(i) = 1; });
    Kokkos::fence();

    int OVResult = 0;
    Kokkos::parallel_reduce(
        rangePolicy1,
        KOKKOS_LAMBDA(const int i, int& updateMe) { updateMe += offsetV1(i); },
        OVResult);

    Kokkos::fence();
    ASSERT_EQ(OVResult, offsetV1.end(0) - offsetV1.begin(0))
        << "found wrong number of elements in OffsetView that was summed.";
  }
  {  // test deep copy of scalar const value into mirro
    const int constVal = 6;
    typename offset_view_type::HostMirror hostOffsetView =
        Kokkos::create_mirror_view(ov);

    Kokkos::deep_copy(hostOffsetView, constVal);

    for (int i = hostOffsetView.begin(0); i < hostOffsetView.end(0); ++i) {
      for (int j = hostOffsetView.begin(1); j < hostOffsetView.end(1); ++j) {
        ASSERT_EQ(hostOffsetView(i, j), constVal)
            << "Bad data found in OffsetView";
      }
    }
  }

  const int ovmin0 = ov.begin(0);
  const int ovend0 = ov.end(0);
  const int ovmin1 = ov.begin(1);
  const int ovend1 = ov.end(1);

  using range_type =
      Kokkos::MDRangePolicy<Device, Kokkos::Rank<2>, Kokkos::IndexType<int> >;
  using point_type = typename range_type::point_type;

  range_type rangePolicy2D(point_type{{ovmin0, ovmin1}},
                           point_type{{ovend0, ovend1}});

  const int constValue = 9;
  Kokkos::parallel_for(
      rangePolicy2D,
      KOKKOS_LAMBDA(const int i, const int j) { ov(i, j) = constValue; });

  // test offsetview to offsetviewmirror deep copy
  typename offset_view_type::HostMirror hostOffsetView =
      Kokkos::create_mirror_view(ov);

  Kokkos::deep_copy(hostOffsetView, ov);

  for (int i = hostOffsetView.begin(0); i < hostOffsetView.end(0); ++i) {
    for (int j = hostOffsetView.begin(1); j < hostOffsetView.end(1); ++j) {
      ASSERT_EQ(hostOffsetView(i, j), constValue)
          << "Bad data found in OffsetView";
    }
  }

  int OVResult = 0;
  Kokkos::parallel_reduce(
      rangePolicy2D,
      KOKKOS_LAMBDA(const int i, const int j, int& updateMe) {
        updateMe += ov(i, j);
      },
      OVResult);

  int answer = 0;
  for (int i = ov.begin(0); i < ov.end(0); ++i) {
    for (int j = ov.begin(1); j < ov.end(1); ++j) {
      answer += constValue;
    }
  }

  ASSERT_EQ(OVResult, answer) << "Bad data found in OffsetView";

  {
    offset_view_type ovCopy(ov);
    ASSERT_EQ(ovCopy == ov, true)
        << "Copy constructor or equivalence operator broken";
  }

  {
    offset_view_type ovAssigned = ov;
    ASSERT_EQ(ovAssigned == ov, true)
        << "Assignment operator or equivalence operator broken";
  }

  {  // construct OffsetView from a View plus begins array
    const int extent0 = 100;
    const int extent1 = 200;
    const int extent2 = 300;
    Kokkos::View<Scalar***, Device> view3D("view3D", extent0, extent1, extent2);

    Kokkos::deep_copy(view3D, 1);

    using range3_type = Kokkos::MDRangePolicy<Device, Kokkos::Rank<3>,
                                              Kokkos::IndexType<int64_t> >;
    using point3_type = typename range3_type::point_type;

    typename point3_type::value_type begins0 = -10, begins1 = -20,
                                     begins2 = -30;
    Kokkos::Array<int64_t, 3> begins         = {{begins0, begins1, begins2}};
    Kokkos::Experimental::OffsetView<Scalar***, Device> offsetView3D(view3D,
                                                                     begins);

    range3_type rangePolicy3DZero(point3_type{{0, 0, 0}},
                                  point3_type{{extent0, extent1, extent2}});

    int view3DSum = 0;
    Kokkos::parallel_reduce(
        rangePolicy3DZero,
        KOKKOS_LAMBDA(const int i, const int j, int k, int& updateMe) {
          updateMe += view3D(i, j, k);
        },
        view3DSum);

    range3_type rangePolicy3D(
        point3_type{{begins0, begins1, begins2}},
        point3_type{{begins0 + extent0, begins1 + extent1, begins2 + extent2}});
    int offsetView3DSum = 0;

    Kokkos::parallel_reduce(
        rangePolicy3D,
        KOKKOS_LAMBDA(const int i, const int j, int k, int& updateMe) {
          updateMe += offsetView3D(i, j, k);
        },
        offsetView3DSum);

    ASSERT_EQ(view3DSum, offsetView3DSum)
        << "construction of OffsetView from View and begins array broken.";
  }
  view_type viewFromOV = ov.view();

  ASSERT_EQ(viewFromOV == ov, true)
      << "OffsetView::view() or equivalence operator View == OffsetView broken";

  {
    offset_view_type ovFromV(viewFromOV, {-1, -2});

    ASSERT_EQ(ovFromV == viewFromOV, true)
        << "Construction of OffsetView from View or equivalence operator "
           "OffsetView == View broken";
  }
  {
    offset_view_type ovFromV = viewFromOV;
    ASSERT_EQ(ovFromV == viewFromOV, true)
        << "Construction of OffsetView from View by assignment (implicit "
           "conversion) or equivalence operator OffsetView == View broken";
  }

  {  // test offsetview to view deep copy
    view_type aView("aView", ov.extent(0), ov.extent(1));
    Kokkos::deep_copy(aView, ov);

    int sum = 0;
    Kokkos::parallel_reduce(
        rangePolicy2D,
        KOKKOS_LAMBDA(const int i, const int j, int& updateMe) {
          updateMe += ov(i, j) - aView(i - ov.begin(0), j - ov.begin(1));
        },
        sum);

    ASSERT_EQ(sum, 0) << "deep_copy(view, offsetView) broken.";
  }

  {  // test view to  offsetview deep copy
    view_type aView("aView", ov.extent(0), ov.extent(1));

    Kokkos::deep_copy(aView, 99);
    Kokkos::deep_copy(ov, aView);

    int sum = 0;
    Kokkos::parallel_reduce(
        rangePolicy2D,
        KOKKOS_LAMBDA(const int i, const int j, int& updateMe) {
          updateMe += ov(i, j) - aView(i - ov.begin(0), j - ov.begin(1));
        },
        sum);

    ASSERT_EQ(sum, 0) << "deep_copy(offsetView, view) broken.";
  }
}

template <typename Scalar, typename Device>
void test_offsetview_unmanaged_construction() {
  // Preallocated memory
  Kokkos::View<Scalar, Device> s("s");
  Scalar* ptr = s.data();  // obtain a pointer into the right address space

  {
    // Constructing an OffsetView directly around our preallocated memory
    Kokkos::Array<int64_t, 1> begins1{{2}};
    Kokkos::Array<int64_t, 1> ends1{{3}};
    Kokkos::Experimental::OffsetView<Scalar*, Device> ov1(ptr, begins1, ends1);

    // Constructing an OffsetView around an unmanaged View of our preallocated
    // memory
    Kokkos::View<Scalar*, Device> v1(ptr, ends1[0] - begins1[0]);
    Kokkos::Experimental::OffsetView<Scalar*, Device> ovv1(v1, begins1);

    // They should match
    ASSERT_EQ(ovv1, ov1)
        << "OffsetView unmanaged construction fails for rank 1";
  }

  {
    Kokkos::Array<int64_t, 2> begins2{{-2, -7}};
    Kokkos::Array<int64_t, 2> ends2{{5, -3}};
    Kokkos::Experimental::OffsetView<Scalar**, Device> ov2(ptr, begins2, ends2);

    Kokkos::View<Scalar**, Device> v2(ptr, ends2[0] - begins2[0],
                                      ends2[1] - begins2[1]);
    Kokkos::Experimental::OffsetView<Scalar**, Device> ovv2(v2, begins2);

    ASSERT_EQ(ovv2, ov2)
        << "OffsetView unmanaged construction fails for rank 2";
  }

  {
    Kokkos::Array<int64_t, 3> begins3{{2, 3, 5}};
    Kokkos::Array<int64_t, 3> ends3{{7, 11, 13}};
    Kokkos::Experimental::OffsetView<Scalar***, Device> ovv3(ptr, begins3,
                                                             ends3);

    Kokkos::View<Scalar***, Device> v3(ptr, ends3[0] - begins3[0],
                                       ends3[1] - begins3[1],
                                       ends3[2] - begins3[2]);
    Kokkos::Experimental::OffsetView<Scalar***, Device> ov3(v3, begins3);

    ASSERT_EQ(ovv3, ov3)
        << "OffsetView unmanaged construction fails for rank 3";
  }

  {
    // Test all four public constructor overloads (begins_type x
    // index_list_type)
    Kokkos::Array<int64_t, 1> begins{{-3}};
    Kokkos::Array<int64_t, 1> ends{{2}};

    Kokkos::Experimental::OffsetView<Scalar*, Device> bb(ptr, begins, ends);
    Kokkos::Experimental::OffsetView<Scalar*, Device> bi(ptr, begins, {2});
    Kokkos::Experimental::OffsetView<Scalar*, Device> ib(ptr, {-3}, ends);
    Kokkos::Experimental::OffsetView<Scalar*, Device> ii(ptr, {-3}, {2});

    ASSERT_EQ(bb, bi);
    ASSERT_EQ(bb, ib);
    ASSERT_EQ(bb, ii);
  }
}

template <typename Scalar, typename Device>
void test_offsetview_unmanaged_construction_death() {
  // Preallocated memory
  Kokkos::View<Scalar, Device> s("s");
  Scalar* ptr = s.data();  // obtain a pointer into the right address space

  // Regular expression syntax on Windows is a pain. `.` does not match `\n`.
  // Feel free to make it work if you have time to spare.
#ifdef _WIN32
#define SKIP_REGEX_ON_WINDOWS(REGEX) ""
#else
#define SKIP_REGEX_ON_WINDOWS(REGEX) REGEX
#endif

  {
    using offset_view_type = Kokkos::Experimental::OffsetView<Scalar*, Device>;

    // Range calculations must be positive
    (void)offset_view_type(ptr, {0}, {1});
    (void)offset_view_type(ptr, {0}, {0});
    ASSERT_DEATH(
        offset_view_type(ptr, {0}, {-1}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "\\(ends\\[0\\] \\(-1\\) - begins\\[0\\] \\(0\\)\\) must be "
            "non-negative"));
  }

  {
    using offset_view_type = Kokkos::Experimental::OffsetView<Scalar*, Device>;

    // Range calculations must not overflow
    (void)offset_view_type(ptr, {0}, {0x7fffffffffffffffl});
    ASSERT_DEATH(
        offset_view_type(ptr, {-1}, {0x7fffffffffffffffl}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "\\(ends\\[0\\] \\(9223372036854775807\\) - begins\\[0\\] "
            "\\(-1\\)\\) "
            "overflows"));
    ASSERT_DEATH(
        offset_view_type(ptr, {-0x7fffffffffffffffl - 1},
                         {0x7fffffffffffffffl}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "\\(ends\\[0\\] \\(9223372036854775807\\) - begins\\[0\\] "
            "\\(-9223372036854775808\\)\\) "
            "overflows"));
    ASSERT_DEATH(
        offset_view_type(ptr, {-0x7fffffffffffffffl - 1}, {0}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "\\(ends\\[0\\] \\(0\\) - begins\\[0\\] "
            "\\(-9223372036854775808\\)\\) "
            "overflows"));
  }

  {
    using offset_view_type = Kokkos::Experimental::OffsetView<Scalar**, Device>;

    // Should throw when the rank of begins and/or ends doesn't match that
    // of OffsetView
    ASSERT_DEATH(
        offset_view_type(ptr, {0}, {1}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "begins\\.size\\(\\) \\(1\\) != Rank \\(2\\)"
            ".*"
            "ends\\.size\\(\\) \\(1\\) != Rank \\(2\\)"));
    ASSERT_DEATH(
        offset_view_type(ptr, {0}, {1, 1}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "begins\\.size\\(\\) \\(1\\) != Rank \\(2\\)"));
    ASSERT_DEATH(
        offset_view_type(ptr, {0}, {1, 1, 1}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "begins\\.size\\(\\) \\(1\\) != Rank \\(2\\)"
            ".*"
            "ends\\.size\\(\\) \\(3\\) != Rank \\(2\\)"));
    ASSERT_DEATH(
        offset_view_type(ptr, {0, 0}, {1}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "ends\\.size\\(\\) \\(1\\) != Rank \\(2\\)"));
    (void)offset_view_type(ptr, {0, 0}, {1, 1});
    ASSERT_DEATH(
        offset_view_type(ptr, {0, 0}, {1, 1, 1}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "ends\\.size\\(\\) \\(3\\) != Rank \\(2\\)"));
    ASSERT_DEATH(
        offset_view_type(ptr, {0, 0, 0}, {1}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "begins\\.size\\(\\) \\(3\\) != Rank \\(2\\)"
            ".*"
            "ends\\.size\\(\\) \\(1\\) != Rank \\(2\\)"));
    ASSERT_DEATH(
        offset_view_type(ptr, {0, 0, 0}, {1, 1}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "begins\\.size\\(\\) \\(3\\) != Rank \\(2\\)"));
    ASSERT_DEATH(
        offset_view_type(ptr, {0, 0, 0}, {1, 1, 1}),
        SKIP_REGEX_ON_WINDOWS(
            "Kokkos::Experimental::OffsetView ERROR: for unmanaged OffsetView"
            ".*"
            "begins\\.size\\(\\) \\(3\\) != Rank \\(2\\)"
            ".*"
            "ends\\.size\\(\\) \\(3\\) != Rank \\(2\\)"));
  }
#undef SKIP_REGEX_ON_WINDOWS
}

template <typename Scalar, typename Device>
void test_offsetview_subview() {
  {  // test subview 1
    Kokkos::Experimental::OffsetView<Scalar*, Device> sliceMe("offsetToSlice",
                                                              {-10, 20});
    {
      auto offsetSubview = Kokkos::Experimental::subview(sliceMe, 0);
      ASSERT_EQ(offsetSubview.rank(), 0u) << "subview of offset is broken.";
    }
  }
  {  // test subview 2
    Kokkos::Experimental::OffsetView<Scalar**, Device> sliceMe(
        "offsetToSlice", {-10, 20}, {-20, 30});
    {
      auto offsetSubview =
          Kokkos::Experimental::subview(sliceMe, Kokkos::ALL(), -2);
      ASSERT_EQ(offsetSubview.rank(), 1u) << "subview of offset is broken.";
    }

    {
      auto offsetSubview =
          Kokkos::Experimental::subview(sliceMe, 0, Kokkos::ALL());
      ASSERT_EQ(offsetSubview.rank(), 1u) << "subview of offset is broken.";
    }
  }

  {  // test subview rank 3

    Kokkos::Experimental::OffsetView<Scalar***, Device> sliceMe(
        "offsetToSlice", {-10, 20}, {-20, 30}, {-30, 40});

    // slice 1
    {
      auto offsetSubview = Kokkos::Experimental::subview(sliceMe, Kokkos::ALL(),
                                                         Kokkos::ALL(), 0);
      ASSERT_EQ(offsetSubview.rank(), 2u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview = Kokkos::Experimental::subview(sliceMe, Kokkos::ALL(),
                                                         0, Kokkos::ALL());
      ASSERT_EQ(offsetSubview.rank(), 2u) << "subview of offset is broken.";
    }

    {
      auto offsetSubview = Kokkos::Experimental::subview(
          sliceMe, 0, Kokkos::ALL(), Kokkos::ALL());
      ASSERT_EQ(offsetSubview.rank(), 2u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview = Kokkos::Experimental::subview(
          sliceMe, 0, Kokkos::ALL(), Kokkos::make_pair(-30, -21));
      ASSERT_EQ(offsetSubview.rank(), 2u) << "subview of offset is broken.";

      ASSERT_EQ(offsetSubview.begin(0), -20);
      ASSERT_EQ(offsetSubview.end(0), 31);
      ASSERT_EQ(offsetSubview.begin(1), 0);
      ASSERT_EQ(offsetSubview.end(1), 9);

      using range_type = Kokkos::MDRangePolicy<Device, Kokkos::Rank<2>,
                                               Kokkos::IndexType<int> >;
      using point_type = typename range_type::point_type;

      const int b0 = offsetSubview.begin(0);
      const int b1 = offsetSubview.begin(1);

      const int e0 = offsetSubview.end(0);
      const int e1 = offsetSubview.end(1);

      range_type rangeP2D(point_type{{b0, b1}}, point_type{{e0, e1}});

      Kokkos::parallel_for(
          rangeP2D,
          KOKKOS_LAMBDA(const int i, const int j) { offsetSubview(i, j) = 6; });

      int sum = 0;
      Kokkos::parallel_reduce(
          rangeP2D,
          KOKKOS_LAMBDA(const int i, const int j, int& updateMe) {
            updateMe += offsetSubview(i, j);
          },
          sum);

      ASSERT_EQ(sum, 6 * (e0 - b0) * (e1 - b1));
    }

    // slice 2
    {
      auto offsetSubview =
          Kokkos::Experimental::subview(sliceMe, Kokkos::ALL(), 0, 0);
      ASSERT_EQ(offsetSubview.rank(), 1u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview =
          Kokkos::Experimental::subview(sliceMe, 0, 0, Kokkos::ALL());
      ASSERT_EQ(offsetSubview.rank(), 1u) << "subview of offset is broken.";
    }

    {
      auto offsetSubview =
          Kokkos::Experimental::subview(sliceMe, 0, Kokkos::ALL(), 0);
      ASSERT_EQ(offsetSubview.rank(), 1u) << "subview of offset is broken.";
    }
  }

  {  // test subview rank 4

    Kokkos::Experimental::OffsetView<Scalar****, Device> sliceMe(
        "offsetToSlice", {-10, 20}, {-20, 30}, {-30, 40}, {-40, 50});

    // slice 1
    {
      auto offsetSubview = Kokkos::Experimental::subview(
          sliceMe, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL(), 0);
      ASSERT_EQ(offsetSubview.rank(), 3u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview = Kokkos::Experimental::subview(
          sliceMe, Kokkos::ALL(), Kokkos::ALL(), 0, Kokkos::ALL());
      ASSERT_EQ(offsetSubview.rank(), 3u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview = Kokkos::Experimental::subview(
          sliceMe, Kokkos::ALL(), 0, Kokkos::ALL(), Kokkos::ALL());
      ASSERT_EQ(offsetSubview.rank(), 3u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview = Kokkos::Experimental::subview(
          sliceMe, 0, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL());
      ASSERT_EQ(offsetSubview.rank(), 3u) << "subview of offset is broken.";
    }

    // slice 2
    auto offsetSubview2a = Kokkos::Experimental::subview(sliceMe, Kokkos::ALL(),
                                                         Kokkos::ALL(), 0, 0);
    ASSERT_EQ(offsetSubview2a.rank(), 2u) << "subview of offset is broken.";
    {
      auto offsetSubview2b = Kokkos::Experimental::subview(
          sliceMe, Kokkos::ALL(), 0, Kokkos::ALL(), 0);
      ASSERT_EQ(offsetSubview2b.rank(), 2u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview2b = Kokkos::Experimental::subview(
          sliceMe, Kokkos::ALL(), 0, 0, Kokkos::ALL());
      ASSERT_EQ(offsetSubview2b.rank(), 2u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview2b = Kokkos::Experimental::subview(
          sliceMe, 0, Kokkos::ALL(), 0, Kokkos::ALL());
      ASSERT_EQ(offsetSubview2b.rank(), 2u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview2b = Kokkos::Experimental::subview(
          sliceMe, 0, 0, Kokkos::ALL(), Kokkos::ALL());
      ASSERT_EQ(offsetSubview2b.rank(), 2u) << "subview of offset is broken.";
    }
    // slice 3
    {
      auto offsetSubview =
          Kokkos::Experimental::subview(sliceMe, Kokkos::ALL(), 0, 0, 0);
      ASSERT_EQ(offsetSubview.rank(), 1u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview =
          Kokkos::Experimental::subview(sliceMe, 0, Kokkos::ALL(), 0, 0);
      ASSERT_EQ(offsetSubview.rank(), 1u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview =
          Kokkos::Experimental::subview(sliceMe, 0, 0, Kokkos::ALL(), 0);
      ASSERT_EQ(offsetSubview.rank(), 1u) << "subview of offset is broken.";
    }
    {
      auto offsetSubview =
          Kokkos::Experimental::subview(sliceMe, 0, 0, 0, Kokkos::ALL());
      ASSERT_EQ(offsetSubview.rank(), 1u) << "subview of offset is broken.";
    }
  }
}

template <class InputIt, class T, class BinaryOperation>
KOKKOS_INLINE_FUNCTION T std_accumulate(InputIt first, InputIt last, T init,
                                        BinaryOperation op) {
  for (; first != last; ++first) {
    init = op(std::move(init), *first);
  }
  return init;
}

KOKKOS_INLINE_FUNCTION int element(std::initializer_list<int> il) {
  return std_accumulate(il.begin(), il.end(), 0,
                        [](int l, int r) { return l * 10 + r; });
}

template <typename DEVICE>
void test_offsetview_offsets_rank1() {
  using data_type        = int*;
  using view_type        = Kokkos::View<data_type, DEVICE>;
  using index_type       = Kokkos::IndexType<int>;
  using execution_policy = Kokkos::RangePolicy<DEVICE, index_type>;
  using offset_view_type = Kokkos::Experimental::OffsetView<data_type, DEVICE>;

  view_type v("View1", 10);
  Kokkos::parallel_for(
      "For1", execution_policy(0, v.extent_int(0)),
      KOKKOS_LAMBDA(const int i) { v(i) = element({i}); });

  int errors;
  Kokkos::parallel_reduce(
      "Reduce1", execution_policy(-3, 4),
      KOKKOS_LAMBDA(const int ii, int& lerrors) {
        offset_view_type ov(v, {ii});
        lerrors += (ov(3) != element({3 - ii}));
        lerrors += (ov[3] != element({3 - ii}));
      },
      errors);

  ASSERT_EQ(0, errors);
}

template <typename DEVICE>
void test_offsetview_offsets_rank2() {
  using data_type        = int**;
  using view_type        = Kokkos::View<data_type, DEVICE>;
  using index_type       = Kokkos::IndexType<int>;
  using execution_policy = Kokkos::RangePolicy<DEVICE, index_type>;
  using offset_view_type = Kokkos::Experimental::OffsetView<data_type, DEVICE>;

  view_type v("View2", 10, 10);
  Kokkos::parallel_for(
      "For2", execution_policy(0, v.extent_int(0)), KOKKOS_LAMBDA(const int i) {
        for (int j = 0; j != v.extent_int(1); ++j) {
          v(i, j) = element({i, j});
        }
      });

  int errors;
  Kokkos::parallel_reduce(
      "Reduce2", execution_policy(-3, 4),
      KOKKOS_LAMBDA(const int ii, int& lerrors) {
        for (int jj = -3; jj <= 3; ++jj) {
          offset_view_type ov(v, {ii, jj});
          lerrors += (ov(3, 3) != element({3 - ii, 3 - jj}));
        }
      },
      errors);

  ASSERT_EQ(0, errors);
}

template <typename DEVICE>
void test_offsetview_offsets_rank3() {
  using data_type        = int***;
  using view_type        = Kokkos::View<data_type, DEVICE>;
  using index_type       = Kokkos::IndexType<int>;
  using execution_policy = Kokkos::RangePolicy<DEVICE, index_type>;
  using offset_view_type = Kokkos::Experimental::OffsetView<data_type, DEVICE>;

  view_type v("View3", 10, 10, 10);
  Kokkos::parallel_for(
      "For3", execution_policy(0, v.extent_int(0)), KOKKOS_LAMBDA(const int i) {
        for (int j = 0; j != v.extent_int(1); ++j) {
          for (int k = 0; k != v.extent_int(2); ++k) {
            v(i, j, k) = element({i, j, k});
          }
        }
      });

  int errors;
  Kokkos::parallel_reduce(
      "Reduce3", execution_policy(-3, 4),
      KOKKOS_LAMBDA(const int ii, int& lerrors) {
        for (int jj = -3; jj <= 3; ++jj) {
          for (int kk = -3; kk <= 3; ++kk) {
            offset_view_type ov(v, {ii, jj, kk});
            lerrors += (ov(3, 3, 3) != element({3 - ii, 3 - jj, 3 - kk}));
          }
        }
      },
      errors);

  ASSERT_EQ(0, errors);
}

TEST(TEST_CATEGORY, offsetview_construction) {
  test_offsetview_construction<int, TEST_EXECSPACE>();
}

TEST(TEST_CATEGORY, offsetview_unmanaged_construction) {
  test_offsetview_unmanaged_construction<int, TEST_EXECSPACE>();
}

TEST(TEST_CATEGORY_DEATH, offsetview_unmanaged_construction) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  test_offsetview_unmanaged_construction_death<int, TEST_EXECSPACE>();
}

TEST(TEST_CATEGORY, offsetview_subview) {
  test_offsetview_subview<int, TEST_EXECSPACE>();
}

TEST(TEST_CATEGORY, offsetview_offsets_rank1) {
  test_offsetview_offsets_rank1<TEST_EXECSPACE>();
}

TEST(TEST_CATEGORY, offsetview_offsets_rank2) {
  test_offsetview_offsets_rank2<TEST_EXECSPACE>();
}

TEST(TEST_CATEGORY, offsetview_offsets_rank3) {
  test_offsetview_offsets_rank3<TEST_EXECSPACE>();
}

}  // namespace Test

#endif /* CONTAINERS_UNIT_TESTS_TESTOFFSETVIEW_HPP_ */