Compadre  1.5.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UtilityTest.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Compadre: COMpatible PArticle Discretization and REmap Toolkit
4 //
5 // Copyright 2018 NTESS and the Compadre contributors.
6 // SPDX-License-Identifier: BSD-2-Clause
7 // *****************************************************************************
8 // @HEADER
9 #include <iostream>
10 #include <string>
11 #include <vector>
12 #include <map>
13 #include <stdlib.h>
14 #include <cstdio>
15 #include <random>
16 
17 #include <Compadre_Config.h>
18 #include <Compadre_GMLS.hpp>
19 #include <Compadre_Evaluator.hpp>
21 
22 #include "GMLS_Tutorial.hpp"
23 
24 #ifdef COMPADRE_USE_MPI
25 #include <mpi.h>
26 #endif
27 
28 #include <Kokkos_Timer.hpp>
29 #include <Kokkos_Core.hpp>
30 
31 using namespace Compadre;
32 
33 //! [Parse Command Line Arguments]
34 
35 // called from command line
36 int main (int argc, char* args[]) {
37 
38 #ifdef COMPADRE_USE_MPI
39 // initialized MPI (if avaialble) with command line arguments given
40 MPI_Init(&argc, &args);
41 #endif
42 
43 // initializes Kokkos with command line arguments given
44 Kokkos::initialize(argc, args);
45 
46 // becomes false if there is unwanted index in the filtered flags
47 bool all_passed = true;
48 
49 // code block to reduce scope for all Kokkos View allocations
50 // otherwise, Views may be deallocating when we call Kokkos finalize() later
51 {
52  // set the number of columns
53  int num_cols = 50; // default 50 columns
54  if (argc >= 3) {
55  if (args[2] != NULL) {
56  auto arg3toi = atoi(args[2]);
57  if (isdigit(arg3toi)) {
58  if (arg3toi > 0) {
59  num_cols = arg3toi;
60  }
61  }
62  }
63  }
64 
65  // set the number of flags
66  int num_flags = 200; // default 200 flags
67  if (argc >= 2) {
68  if (args[1] != NULL) {
69  auto arg2toi = atoi(args[1]);
70  if (isdigit(arg2toi)) {
71  if (arg2toi > 0) {
72  num_flags = arg2toi;
73  }
74  }
75  }
76  }
77  //! [Parse Command Line Arguments]
78 
79  //! [Setting Up Data]
80  Kokkos::Timer timer;
81  Kokkos::Profiling::pushRegion("Setup Data");
82 
83  // create a 2D view of inputs
84  Kokkos::View<int**, Kokkos::DefaultExecutionSpace> data_device("data", num_flags, num_cols);
85  Kokkos::View<int**>::HostMirror data = Kokkos::create_mirror_view(data_device);
86 
87  // create a view of flags
88  Kokkos::View<int*, Kokkos::DefaultExecutionSpace> flags_device("flags", num_flags);
89  Kokkos::View<int*>::HostMirror flags = Kokkos::create_mirror_view(flags_device);
90 
91  //! [Setting Up Data]
92 
93  Kokkos::Profiling::popRegion();
94  Kokkos::Profiling::pushRegion("Filter And Extract Data");
95 
96  //! [Filtering And Extracting Data]
97  // create arbitrary data
98  for (int i=0; i<num_flags; i++) {
99  for (int j=0; j<num_cols; j++) {
100  if ((i % 2) == 0) {
101  data(i, j) = 1;
102  } else {
103  data(i, j) = 0;
104  }
105  }
106  }
107  // copy the data from host to device
108  Kokkos::deep_copy(data_device, data);
109 
110  // create arbitrary flags
111  int num_filtered_flags = 0; // number of filtered flags
112  for (int i=0; i<num_flags; i++) {
113  if ((i % 2) == 0) {
114  flags(i) = 1;
115  num_filtered_flags++;
116  } else {
117  flags(i) = 0;
118  }
119  }
120  // copy the flags from host to device
121  Kokkos::deep_copy(flags_device, flags);
122 
123  // Then call out the function to create view
124  auto filtered_flags = filterViewByID<Kokkos::HostSpace>(flags_device, 1);
125  auto extracted_data = Extract::extractViewByIndex<Kokkos::HostSpace>(data_device, filtered_flags);
126 
127  //! [Filtering Data]
128 
129  Kokkos::Profiling::popRegion();
130  Kokkos::Profiling::pushRegion("Check Filtered And Extracted Data");
131 
132  //! [Checking Filtered And Extracted Data]
133 
134  if (filtered_flags.extent(0) != (size_t)num_filtered_flags) {
135  all_passed = false;
136  std::cout << "Failed - number of filtered flags not matched!" << filtered_flags.extent(0) << " " << num_filtered_flags << std::endl;
137  }
138  for (size_t i=0; i<filtered_flags.extent(0); i++) {
139  if (filtered_flags(i) % 2 != 0) {
140  all_passed = false;
141  std::cout << "Failed - incorrect filtered flags " << filtered_flags(i) << std::endl;
142  }
143  }
144  // All values inside extracted data should now be 1
145  for (size_t i=0; i<extracted_data.extent(0); i++) {
146  for (size_t j=0; j<extracted_data.extent(1); j++) {
147  if (extracted_data(i, j) != 1) {
148  all_passed = false;
149  std::cout << "Failed - incorrect values in extracted view at index " << i << " " << j << " " << extracted_data(i, j) << std::endl;
150  }
151  }
152  }
153 
154  //! [Checking Filtered And Extracted Data]
155  // stop timing comparison loop
156  Kokkos::Profiling::popRegion();
157  //! [Finalize Program]
158 
159 } // end of code block to reduce scope, causing Kokkos View de-allocations
160 // otherwise, Views may be deallocating when we call Kokkos finalize() later
161 
162 // finalize Kokkos and MPI (if available)
163 Kokkos::finalize();
164 #ifdef COMPADRE_USE_MPI
165 MPI_Finalize();
166 #endif
167 
168 // output to user that test passed or failed
169 if (all_passed) {
170  fprintf(stdout, "Passed test \n");
171  return 0;
172 } else {
173  fprintf(stdout, "Failed test \n");
174  return -1;
175 }
176 
177 } // main
178 
179 //! [Finalize Program]
int main(int argc, char **argv)