Skip to content

Commit 5c43467

Browse files
pcaspersjenkins
authored andcommitted
QPR-12389 Additional Unit Test For GPGPU Framework
1 parent 2605b1b commit 5c43467

1 file changed

Lines changed: 196 additions & 0 deletions

File tree

QuantExt/test/computeenvironment.cpp

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,202 @@ BOOST_AUTO_TEST_CASE(testReplayFlowError) {
251251
}
252252
}
253253

254+
// The code below replicates the behavior of one iteration of one of our static backtests
255+
// when executed on our GPGPU Framework.
256+
namespace {
257+
double externalAverage(const std::vector<double>& v) {
258+
boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::mean>> acc;
259+
for (auto const& x : v)
260+
acc(x);
261+
return boost::accumulators::mean(acc);
262+
}
263+
struct S0 {
264+
std::size_t randomVariableOpCode;
265+
std::vector<std::size_t> args;
266+
std::vector<bool> vb;
267+
};
268+
void runBacktest(const std::string &d, const std::size_t id, const std::vector<S0>& v0, double expected) {
269+
270+
BOOST_TEST_MESSAGE("testing backtest iteration on device '" << d << "'.");
271+
ComputeEnvironment::instance().selectContext(d);
272+
273+
const std::size_t n = 10000;
274+
std::size_t externalCalculationId_ = 0;
275+
std::size_t cgVersion_ = 1;
276+
bool newExternalCalc = false;
277+
std::tie(externalCalculationId_, newExternalCalc) =
278+
ComputeEnvironment::instance().context().initiateCalculation(n, externalCalculationId_, cgVersion_);
279+
constexpr double max = std::numeric_limits<float>::max();
280+
281+
std::vector<double> v1 = {
282+
-56340, -39240, -37560, -37380, -35460, -30960, -26220, -26160, -24920, -24060, -22820,
283+
-22680, -20640, -18780, -18540, -17480, -16680, -15960, -15380, -15120, -14100, -13380,
284+
-13140, -13080, -13020, -12460, -11120, -10640, -10320, -9480, -9400, -9020, -8920,
285+
-8760, -8740, -8540, -7560, -7300, -7260, -6820, -6320, -5560, -5320, -4820,
286+
-4700, -4460, -4380, -3160, -2, -1, -0.0939, -0.0654, -0.0623, -0.0516, -0.0437,
287+
-0.0378, -0.0278, -0.0266, -0.0235, -0.0223, -0.0219, -0.0158, 0, 0.003, 0.0075, 0.5,
288+
0.8434, 0.8719, 0.875, 0.8857, 0.8936, 0.8995, 0.9095, 0.9107, 0.9138, 0.915, 0.9154,
289+
0.9215, 0.9373, 0.9403, 0.9448, 1, 2, 3, 15, 30, 600, 1200,
290+
1500, 2100, 3000, 30000, 187460, 200000, 400000, max, 0.0242777, 0.000589407, 0.99513,
291+
0.998575, -0.172332, 0.998575};
292+
293+
for (const auto& x : v1)
294+
ComputeEnvironment::instance().context().createInputVariable(x);
295+
296+
std::vector<std::vector<std::size_t>> rv = {{11}};
297+
auto gen = ComputeEnvironment::instance().context().createInputVariates(rv.size(), rv.front().size(), 42);
298+
299+
for (const auto& s : v0) {
300+
ComputeEnvironment::instance().context().applyOperation(s.randomVariableOpCode, s.args);
301+
int i = 0;
302+
for (const auto& b : s.vb) {
303+
if (b)
304+
ComputeEnvironment::instance().context().freeVariable(s.args[i]);
305+
i++;
306+
}
307+
}
308+
ComputeEnvironment::instance().context().declareOutputVariable(id);
309+
310+
std::vector<std::vector<double>> externalOutput_ = std::vector<std::vector<double>>(1, std::vector<double>(n));
311+
std::vector<double*> externalOutputPtr_ = std::vector<double*>(1, &externalOutput_.front()[0]);
312+
std::size_t regressionOrder = 2;
313+
ComputeEnvironment::instance().context().finalizeCalculation(externalOutputPtr_, {regressionOrder});
314+
auto baseNpv_ = externalAverage(externalOutput_[0]);
315+
BOOST_CHECK_CLOSE(baseNpv_, expected, 1E-3);
316+
}
317+
} // namespace
318+
319+
BOOST_AUTO_TEST_CASE(testBacktest) {
320+
321+
ComputeEnvironmentFixture fixture;
322+
std::size_t id;
323+
double expected;
324+
std::vector<S0> v;
325+
for (auto const& d : ComputeEnvironment::instance().getAvailableDevices()) {
326+
327+
if (d == "BasicCpu/Default/Default") {
328+
id = 78;
329+
expected = -39400;
330+
v = {
331+
{5, {99, 98}, {false, false}},
332+
{4, {65, 97}, {true, false}},
333+
{15, {103}, {true}},
334+
{3, {65}, {true}},
335+
{2, {103, 104}, {true, true}},
336+
{13, {100}, {false}},
337+
{4, {96, 102}, {false, true}},
338+
{1, {100, 103}, {false, true}},
339+
{1, {105, 65}, {true, true}},
340+
{13, {103}, {true}},
341+
{8, {65, 62}, {false, false}},
342+
{8, {65, 78}, {false, false}},
343+
{2, {81, 105}, {false, true}},
344+
{4, {103, 106}, {true, true}},
345+
{2, {65, 78}, {false, false}},
346+
{4, {94, 106}, {true, true}},
347+
{4, {105, 103}, {false, true}},
348+
{2, {81, 105}, {false, true}},
349+
{8, {65, 78}, {false, false}},
350+
{8, {65, 95}, {false, true}},
351+
{2, {81, 94}, {false, true}},
352+
{4, {105, 95}, {true, true}},
353+
{2, {65, 78}, {true, true}},
354+
{4, {93, 95}, {true, true}},
355+
{1, {106, 78}, {false, true}},
356+
{4, {94, 95}, {false, true}},
357+
{2, {81, 94}, {false, true}},
358+
{4, {95, 106}, {true, true}},
359+
{1, {78, 94}, {true, true}},
360+
{9, {106, 62}, {false, true}},
361+
{1, {89, 106}, {false, false}},
362+
{4, {94, 62}, {false, true}},
363+
{2, {81, 94}, {false, false}},
364+
{4, {62, 89}, {true, true}},
365+
{1, {78, 95}, {true, true}},
366+
{4, {94, 83}, {false, true}},
367+
{2, {81, 94}, {false, true}},
368+
{4, {83, 82}, {true, true}},
369+
{1, {95, 94}, {true, true}},
370+
{9, {89, 91}, {true, true}},
371+
{2, {81, 94}, {false, false}},
372+
{2, {81, 94}, {false, false}},
373+
{5, {81, 101}, {false, false}},
374+
{5, {81, 95}, {false, true}},
375+
{4, {106, 83}, {false, false}},
376+
{4, {94, 95}, {false, true}},
377+
{2, {81, 94}, {false, false}},
378+
{2, {81, 94}, {false, true}},
379+
{4, {106, 83}, {true, true}},
380+
{1, {78, 94}, {false, true}},
381+
{4, {62, 83}, {false, true}},
382+
{2, {81, 62}, {true, true}},
383+
{4, {83, 78}, {true, true}},
384+
{1, {94, 62}, {true, true}},
385+
};
386+
} else {
387+
id = 111;
388+
expected = -39438;
389+
v = {
390+
{5, {99, 98}, {false, false}},
391+
{4, {65, 97}, {true, false}},
392+
{15, {103}, {true}},
393+
{3, {105}, {true}},
394+
{2, {103, 104}, {true, true}},
395+
{13, {100}, {false}},
396+
{4, {96, 102}, {false, true}},
397+
{1, {100, 103}, {false, true}},
398+
{1, {102, 105}, {true, true}},
399+
{13, {103}, {true}},
400+
{8, {105, 62}, {false, false}},
401+
{8, {105, 78}, {false, false}},
402+
{2, {81, 102}, {false, true}},
403+
{4, {103, 106}, {true, true}},
404+
{2, {105, 78}, {false, false}},
405+
{4, {94, 106}, {true, true}},
406+
{4, {102, 103}, {false, true}},
407+
{2, {81, 102}, {false, true}},
408+
{8, {105, 78}, {false, false}},
409+
{8, {105, 95}, {false, true}},
410+
{2, {81, 107}, {false, true}},
411+
{4, {102, 108}, {true, true}},
412+
{2, {105, 78}, {true, true}},
413+
{4, {93, 108}, {true, true}},
414+
{1, {106, 105}, {false, true}},
415+
{4, {107, 108}, {false, true}},
416+
{2, {81, 107}, {false, true}},
417+
{4, {108, 106}, {true, true}},
418+
{1, {105, 107}, {true, true}},
419+
{9, {106, 62}, {false, true}},
420+
{1, {89, 106}, {false, false}},
421+
{4, {107, 105}, {false, true}},
422+
{2, {81, 107}, {false, false}},
423+
{4, {105, 89}, {true, true}},
424+
{1, {108, 102}, {true, true}},
425+
{4, {107, 83}, {false, true}},
426+
{2, {81, 107}, {false, true}},
427+
{4, {108, 82}, {true, true}},
428+
{1, {102, 107}, {true, true}},
429+
{9, {105, 91}, {true, true}},
430+
{2, {81, 107}, {false, false}},
431+
{2, {81, 107}, {false, false}},
432+
{5, {81, 101}, {false, false}},
433+
{5, {81, 109}, {false, true}},
434+
{4, {106, 110}, {false, false}},
435+
{4, {107, 109}, {false, true}},
436+
{2, {81, 107}, {false, false}},
437+
{2, {81, 107}, {false, true}},
438+
{4, {106, 110}, {true, true}},
439+
{1, {111, 107}, {false, true}},
440+
{4, {112, 110}, {false, true}},
441+
{2, {81, 112}, {true, true}},
442+
{4, {110, 111}, {true, true}},
443+
{1, {107, 112}, {true, true}},
444+
};
445+
}
446+
runBacktest(d, id, v, expected);
447+
}
448+
}
449+
254450
BOOST_AUTO_TEST_SUITE_END()
255451

256452
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)