test_finance.py 2.09 KB
Newer Older
root's avatar
root committed
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
import os
import re
import unittest

from example_tests import example_test

from cupy import testing


def _normalize_regexp_eol(pattern):
    return pattern.replace(r'\n', re.escape(os.linesep))


class TestBlackScholes(unittest.TestCase):

    def test_black_scholes(self):
        output = example_test.run_example(
            'finance/black_scholes.py', '--n-options', '10')
        pattern = _normalize_regexp_eol(
            r'initializing...\n' +
            r'start computation\n' +
            r' CPU \(NumPy, Naive implementation\):\t[0-9\.]+ sec\n' +
            r' GPU \(CuPy, Naive implementation\):\t[0-9\.]+ sec\n' +
            r' GPU \(CuPy, Elementwise kernel\):\t[0-9\.]+ sec')
        assert re.search(pattern, output.decode('utf-8'))


class TestMonteCarlo(unittest.TestCase):

    def test_monte_carlo(self):
        output = example_test.run_example(
            'finance/monte_carlo.py', '--n-options', '10',
            '--n-samples-per-thread', '10',
            '--n-threads-per-option', '10')
        pattern = _normalize_regexp_eol(
            r'initializing...\n' +
            r'start computation\n' +
            r'    # of options: 10\n' +
            r'    # of samples per option: 100\n' +
            r'GPU \(CuPy, Monte Carlo method\):\t[0-9\.]+ sec\n' +
            r'Error: [0-9\.]+')
        assert re.search(pattern, output.decode('utf-8'))


class TestMonteCarloWithMultiGPU(unittest.TestCase):

    @testing.multi_gpu(2)
    def test_monte_carlo_multigpu(self):
        output = example_test.run_example(
            'finance/monte_carlo_multigpu.py', '--gpus', '0', '1',
            '--n-options', '10',
            '--n-samples-per-thread', '10',
            '--n-threads-per-option', '10')
        pattern = _normalize_regexp_eol(
            r'initializing...\n' +
            r'start computation\n' +
            r'    # of gpus: 2\n' +
            r'    # of options: 10\n' +
            r'    # of samples per option: 200\n' +
            r'GPU \(CuPy, Monte Carlo method\):\t[0-9\.]+ sec\n' +
            r'Error: [0-9\.]+')
        assert re.search(pattern, output.decode('utf-8'))