|
| 1 | +#include <cstdlib> |
| 2 | +#include <iomanip> |
| 3 | +#include <iostream> |
| 4 | +#include <random> |
| 5 | + |
| 6 | +#include "AMS.h" |
| 7 | + |
| 8 | +using real_t = double; |
| 9 | +using namespace ams; |
| 10 | + |
| 11 | +void eval(real_t *density, |
| 12 | + real_t *e_mass, |
| 13 | + real_t *qc, |
| 14 | + real_t deltaTime, |
| 15 | + real_t **mat, |
| 16 | + int NumComps, |
| 17 | + int NumZones) |
| 18 | +{ |
| 19 | + // Density is a 0->vector. |
| 20 | + real_t *Dense = density; |
| 21 | + real_t *eMass = e_mass; |
| 22 | + real_t *QC = qc; |
| 23 | + |
| 24 | + for (int j = 0; j < NumZones; j++) { |
| 25 | + real_t A = Dense[j]; // Reactant A |
| 26 | + for (int i = 0; i < NumComps; i++) { |
| 27 | + real_t k = mat[j][i]; // Reaction rate constant |
| 28 | + real_t reaction_rate = k * A * deltaTime; |
| 29 | + Dense[j] -= reaction_rate; |
| 30 | + eMass[j] = reaction_rate * k; |
| 31 | + QC[j] += reaction_rate; |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void eval_ams(AMSExecutor &wf, |
| 37 | + real_t *density, |
| 38 | + real_t *e_mass, |
| 39 | + real_t *qc, |
| 40 | + real_t deltaTime, |
| 41 | + real_t **mat, |
| 42 | + int NumComps, |
| 43 | + int NumZones) |
| 44 | +{ |
| 45 | + // Density is a 0->vector. |
| 46 | + SmallVector<AMSTensor> input_tensors; |
| 47 | + SmallVector<AMSTensor> inout_tensors; |
| 48 | + SmallVector<AMSTensor> output_tensors; |
| 49 | + // Density is inout. |
| 50 | + inout_tensors.push_back( |
| 51 | + AMSTensor::view(density, |
| 52 | + SmallVector<ams::AMSTensor::IntDimType>({NumZones, 1}), |
| 53 | + SmallVector<ams::AMSTensor::IntDimType>({1, 1}), |
| 54 | + AMSResourceType::AMS_HOST)); |
| 55 | + // QC is inout |
| 56 | + inout_tensors.push_back( |
| 57 | + AMSTensor::view(qc, |
| 58 | + SmallVector<ams::AMSTensor::IntDimType>({NumZones, 1}), |
| 59 | + SmallVector<ams::AMSTensor::IntDimType>({1, 1}), |
| 60 | + AMSResourceType::AMS_HOST)); |
| 61 | + |
| 62 | + input_tensors.push_back(AMSTensor::view( |
| 63 | + &mat[0][0], |
| 64 | + SmallVector<ams::AMSTensor::IntDimType>({NumZones, NumComps}), |
| 65 | + SmallVector<ams::AMSTensor::IntDimType>({NumComps, 1}), |
| 66 | + AMSResourceType::AMS_HOST)); |
| 67 | + |
| 68 | + // deltaTime is a scalar input, I BROADCAST it now with 0 strides. |
| 69 | + input_tensors.push_back( |
| 70 | + AMSTensor::view(&deltaTime, |
| 71 | + SmallVector<ams::AMSTensor::IntDimType>({NumZones, 1}), |
| 72 | + SmallVector<ams::AMSTensor::IntDimType>({0, 0}), |
| 73 | + AMSResourceType::AMS_HOST)); |
| 74 | + |
| 75 | + // e_mass is just an output |
| 76 | + output_tensors.push_back( |
| 77 | + AMSTensor::view(e_mass, |
| 78 | + SmallVector<ams::AMSTensor::IntDimType>({NumZones, 1}), |
| 79 | + SmallVector<ams::AMSTensor::IntDimType>({1, 1}), |
| 80 | + AMSResourceType::AMS_HOST)); |
| 81 | + |
| 82 | + |
| 83 | + EOSLambda OrigComputation = |
| 84 | + [&](const ams::SmallVector<ams::AMSTensor> &ams_ins, |
| 85 | + ams::SmallVector<ams::AMSTensor> &ams_inouts, |
| 86 | + ams::SmallVector<ams::AMSTensor> &ams_outs) { |
| 87 | + int prunedZones = ams_ins[0].shape()[0]; |
| 88 | + std::cout << "Pruned are " << prunedZones << "\n"; |
| 89 | + real_t *pruned_mat[prunedZones]; |
| 90 | + // The 2D data of materials are unnder a c_vector. |
| 91 | + real_t *c_mats = ams_ins[0].data<real_t>(); |
| 92 | + // We need this as eval requires a c like 2D vector |
| 93 | + for (int i = 0; i < prunedZones; i++) { |
| 94 | + pruned_mat[i] = &c_mats[i * ams_ins[0].shape()[1]]; |
| 95 | + } |
| 96 | + eval(ams_inouts[0].data<real_t>(),// density was the first entry in inout |
| 97 | + ams_outs[0].data<real_t>(), |
| 98 | + ams_inouts[1].data<real_t>(), // qc was the second entry in inout |
| 99 | + *ams_ins[1].data<real_t>(), |
| 100 | + pruned_mat, |
| 101 | + NumComps, |
| 102 | + prunedZones); |
| 103 | + }; |
| 104 | + // After I call this, I expect the database to have the following order: |
| 105 | + // input_Data: **input_tensors, **inout_tensors |
| 106 | + // input_Data: **output_tensors, **inout_tensors |
| 107 | + // In this example the database will have the following: |
| 108 | + // Input: |Mat_0|Mat_1|dt|density|qc| Output : |e_mass|density|qc| |
| 109 | + AMSExecute(wf, OrigComputation, input_tensors, inout_tensors, output_tensors); |
| 110 | +} |
| 111 | + |
| 112 | +void initializeRandom(real_t *data, |
| 113 | + size_t NumElements, |
| 114 | + real_t minVal = 0.0, |
| 115 | + real_t maxVal = 1.0) |
| 116 | +{ |
| 117 | + std::random_device rd; |
| 118 | + std::mt19937 gen(0); |
| 119 | + std::uniform_real_distribution<real_t> dist(minVal, maxVal); |
| 120 | + for (size_t i = 0; i < NumElements; i++) { |
| 121 | + data[i] = dist(gen); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +int main(int argc, char *argv[]) |
| 127 | +{ |
| 128 | + int numZones = std::atoi(argv[1]); |
| 129 | + int numComps = std::atoi(argv[2]); |
| 130 | + real_t *actualDensity = new real_t[numZones]; |
| 131 | + initializeRandom(actualDensity, numZones); |
| 132 | + real_t *eMass = new real_t[numZones]; |
| 133 | + initializeRandom(eMass, numZones); |
| 134 | + real_t *qc = new real_t[numZones]; |
| 135 | + initializeRandom(qc, numZones); |
| 136 | + real_t dt = 1.0; |
| 137 | + ams::AMSConfigureFSDatabase(ams::AMSDBType::AMS_HDF5, "./"); |
| 138 | + ams::AMSCAbstrModel model_descr = AMSRegisterAbstractModel( |
| 139 | + "test", ams::AMSUQPolicy::AMS_RANDOM, 0.0, nullptr, "test"); |
| 140 | + ams::AMSExecutor wf = ams::AMSCreateExecutor(model_descr, 0, 1); |
| 141 | + |
| 142 | + // Here I am uncertain if materials are NumComps or NumZones. |
| 143 | + // NOTE: Materials may or may not be contineous on the outer dimension. |
| 144 | + // We take a worst case scenario here, in which data are non contineous. |
| 145 | + real_t *materials[numZones]; |
| 146 | + real_t *tmpData = new real_t[numZones * numComps]; |
| 147 | + for (int i = 0; i < numZones; i++) { |
| 148 | + materials[i] = &tmpData[i * numComps]; |
| 149 | + initializeRandom(materials[i], numComps); |
| 150 | + } |
| 151 | + |
| 152 | +#if 0 |
| 153 | + // THIS WE DO NOT SUPPORT CAUSE the materials data will be a non contineous vector |
| 154 | + real_t *materials[numZones]; |
| 155 | + for (int i = 0; i < numZones; i++) { |
| 156 | + materials[i] = new real_t[numComps]; |
| 157 | + initializeRandom(materials[i], numComps); |
| 158 | + } |
| 159 | +#endif |
| 160 | + std::cout << std::fixed << std::setprecision(2); |
| 161 | + |
| 162 | + std::cout << "Before\n"; |
| 163 | + for (int i = 0; i < numZones; i++) { |
| 164 | + std::cout << "Dense: " << actualDensity[i] << " eMass:" << eMass[i] |
| 165 | + << " QC:" << qc[i]; |
| 166 | + for (int j = 0; j < numComps; j++) { |
| 167 | + std::cout << " Mat_" << j << " " << materials[i][j]; |
| 168 | + } |
| 169 | + std::cout << "\n"; |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + eval_ams(wf, actualDensity, eMass, qc, dt, materials, numComps, numZones); |
| 174 | + |
| 175 | + std::cout << "After\n"; |
| 176 | + for (int i = 0; i < numZones; i++) { |
| 177 | + std::cout << "Dense: " << actualDensity[i] << " eMass:" << eMass[i] |
| 178 | + << " QC:" << qc[i]; |
| 179 | + for (int j = 0; j < numComps; j++) { |
| 180 | + std::cout << " Mat_" << j << " " << materials[i][j]; |
| 181 | + } |
| 182 | + std::cout << "\n"; |
| 183 | + } |
| 184 | +} |
0 commit comments