예제 #1
0
def run_tests():
    bb = 1
    cc = 1
    ee = 1000
    k_num = 10
    a1 = np.random.random((bb, ee, cc))
    a2 = np.random.random((bb, ee, cc))
    for k_ee in range(13, 14):
        b = np.random.random((k_num, k_ee, cc))
        for md in ['valid', 'same', 'full']:
            for orientation in ['as-is', 'flipped']:
                import time
                t1 = time.time()
                y1 = reference(a1, b, md, orientation, 'NEC')
                t2 = time.time()
                y2 = reference(a2, b, md, orientation, 'NEC')
                op = conv_1d(a1, b, mode=md, kernel_orientation=orientation, data_format='NEC')

                # result1 =
                if ovl.cuda_enabled:
                    assert np.allclose(ovl.evaluate(op, target_language='cuda'), y1)
                # for d in range(1):
                a1[:] = a2[:]
                if ovl.cuda_enabled:
                    assert np.allclose(ovl.evaluate(op, target_language='cuda'), y2)
                    res, prof = ovl.profile(op, target_language='cuda', profiling_iterations=100, opt_level=3)
                    ovl.logger.debug(k_ee, md, orientation, (t2 - t1) * 1000, np.min(list(prof.values())[0]))
예제 #2
0
def countTrianglesCPU(startEdge, fromVertex, toVertex):
    """Count the triangles on the CPU.

    The array toVertex is a flattened list of lists structure, where startEdge encodes the start indices of the lists.

    :param startEdge: Indices into toVertex where edges start.
    :type startEdge: list.
    :param fromVertex: The from-vertex of each edge.
    :type fromVertex: list.
    :param toVertex: The to-vertex of each edge.
    :type toVertex: list.
    :return: Triangle count of graph.

    :Examples:

    .. doctest::

        >>> from opveclib.examples.test_graph import loadGraphFromTextFile, writeExampleGraphToTextFile, countTrianglesCPU
        >>> tmpName = "/tmp/v7e20.txt"
        >>> writeExampleGraphToTextFile(tmpName)
        >>> startEdge, fromVertex, toVertex = loadGraphFromTextFile(tmpName)
        >>> countTrianglesCPU(startEdge, fromVertex, toVertex)
        3
    """
    count = ovl.evaluate(graph_triangle_count(startEdge, fromVertex, toVertex),
                         target_language='cpp')
    return np.sum(count, axis=0, dtype=np.uint64)
예제 #3
0
def triangles(startEdge, fromVertex, toVertex, target_language='cpp'):
    """Count the triangles on the GPU.

    The array toVertex is a flattened list of lists structure, where startEdge encodes the start indices of the lists.

    :param startEdge: Indices into toVertex where edges start.
    :type startEdge: list.
    :param fromVertex: The from-vertex of each edge.
    :type fromVertex: list.
    :param toVertex: The to-vertex of each edge.
    :type toVertex: list.
    :return: Triangle count of graph.

    :Examples:

    .. doctest::

        >>> from opveclib.examples.test_graph import load_graph_from_text_file, write_example_graph_to_text_file, triangles
        >>> tmpName = "/tmp/v7e20.txt"
        >>> write_example_graph_to_text_file(tmpName)
        >>> startEdge, fromVertex, toVertex = load_graph_from_text_file(tmpName)
        >>> triangles(startEdge, fromVertex, toVertex)
        3
    """
    if not ovl.local.cuda_enabled and target_language == 'cuda':
        ovl.logger.debug("triangles tried to use cuda which is not enabled.")
        return 0

    count = ovl.evaluate(triangles_op(startEdge, fromVertex, toVertex), target_language=target_language)

    return np.sum(count, axis=0, dtype=np.uint64)
예제 #4
0
    def test(self):
        """
        Test the correctness of ovl operator vs numpy implementation
        """
        a = np.array([1e-10, -1e-10, 0.0, np.Infinity], dtype=np.float64)
        expm1_op = expm1(a)
        ref = np.expm1(a)
        ovl_res = ovl.evaluate(expm1_op)
        ovl.logger.info(u'numpy: ' + str(ref) + u' ovl: ' + str(ovl_res))
        assert np.allclose(ref, ovl_res, rtol=0, atol=1e-20)
        if ovl.cuda_enabled:
            assert np.allclose(np.expm1(a),
                               ovl.evaluate(expm1_op, target_language='cuda'),
                               rtol=0,
                               atol=1e-20)

        # test  vs tensorflow
        # ensure TF runs on GPU when asked
        test_config = tf.ConfigProto(allow_soft_placement=False)
        test_config.graph_options.optimizer_options.opt_level = -1
        ones = np.ones_like(a)
        if ovl.cuda_enabled:
            devices = ['/cpu:0', '/gpu:0']
        else:
            devices = ['/cpu:0']
        with tf.Session(config=test_config) as sess:
            for dev_string in devices:
                with tf.device(dev_string):
                    expm1_tf = ovl.as_tensorflow(expm1_op)
                    sess.run(tf.initialize_all_variables())
                    expm1_tf_result = sess.run(expm1_tf)
                    assert np.allclose(ref,
                                       expm1_tf_result,
                                       rtol=0,
                                       atol=1e-20)

                    # TF exp - 1
                    tf_out = tf.exp(a) - ones
                    tf_result = tf_out.eval()
                    # this should fail
                    assert (np.allclose(ref, tf_result, rtol=0,
                                        atol=1e-20) == False)
        sess.close()
예제 #5
0
    def test(self):
        """
        Test the correctness of ovl operator vs numpy implementation
        """
        a = np.array([1e-99, -1e-99, 0.0, np.Infinity], dtype=np.float64)
        log1pOp = log1p(a)
        ref = np.log1p(a)
        ovl_res = ovl.evaluate(log1pOp)
        ovl.logger.info(u'numpy: ' + str(ref) + u' ovl: ' + str(ovl_res))
        assert np.allclose(ref, ovl_res, rtol=0, atol=1e-20)
        if ovl.cuda_enabled:
            assert np.allclose(np.log1p(a),
                      ovl.evaluate(log1pOp, target_language='cuda'),
                      rtol=0, atol=1e-20)

        # test  vs tensorflow
        test_config=tf.ConfigProto(allow_soft_placement=False)
        # ensure TF runs on GPU when asked
        test_config.graph_options.optimizer_options.opt_level = -1
        ones = np.ones_like(a)
        if ovl.cuda_enabled:
            devices = ['/cpu:0', '/gpu:0']
        else:
            devices = ['/cpu:0']
        with tf.Session(config=test_config) as sess:
           for dev_string in devices:
                with tf.device(dev_string):
                    log1p_tf = ovl.as_tensorflow(log1pOp)
                    sess.run(tf.initialize_all_variables())
                    log1p_tf_result = sess.run(log1p_tf)
                    assert np.allclose(ref, log1p_tf_result,
                                       rtol=0, atol=1e-20)

                    # TF exp - 1
                    tf_out = tf.log(a - ones)
                    tf_result = tf_out.eval()
                    # this should fail
                    assert (np.allclose(ref, tf_result,
                                        rtol=0, atol=1e-20) == False)
        sess.close()
예제 #6
0
def run_tests():
    bb = 1
    cc = 1
    ee = 1000
    k_num = 10
    a1 = np.random.random((bb, ee, cc))
    a2 = np.random.random((bb, ee, cc))
    for k_ee in range(13, 14):
        b = np.random.random((k_num, k_ee, cc))
        for md in ['valid', 'same', 'full']:
            for orientation in ['as-is', 'flipped']:
                import time
                t1 = time.time()
                y1 = reference(a1, b, md, orientation, 'NEC')
                t2 = time.time()
                y2 = reference(a2, b, md, orientation, 'NEC')
                op = conv_1d(a1,
                             b,
                             mode=md,
                             kernel_orientation=orientation,
                             data_format='NEC')

                # result1 =
                assert np.allclose(ovl.evaluate(op, target_language='cuda'),
                                   y1)
                # for d in range(1):
                a1[:] = a2[:]
                assert np.allclose(ovl.evaluate(op, target_language='cuda'),
                                   y2)

                res, prof = ovl.profile(op,
                                        target_language='cuda',
                                        profiling_iterations=100,
                                        opt_level=3)

                # print(prof)
                # print(debug)
                # print(op[0, 0, :])
                ovl.logger.debug(k_ee, md, orientation, (t2 - t1) * 1000,
                                 np.min(list(prof.values())[0]))
예제 #7
0
    def test(self):
        """
        Test the outputs of the operators to make sure they are consistent with the numpy implementation
        """

        a = np.random.random((5, 5, 5))
        ovl.logger.debug(u'Testing C')
        assert np.allclose(
            np.cumsum(a, axis=0),
            ovl.evaluate(cumsum(a, axis=0), target_language='cpp'))
        assert np.allclose(
            np.cumsum(a, axis=1),
            ovl.evaluate(cumsum(a, axis=1), target_language='cpp'))
        assert np.allclose(
            np.cumsum(a, axis=2),
            ovl.evaluate(cumsum(a, axis=2), target_language='cpp'))

        assert np.allclose(
            np.cumprod(a, axis=0),
            ovl.evaluate(cumprod(a, axis=0), target_language='cpp'))
        assert np.allclose(
            np.cumprod(a, axis=1),
            ovl.evaluate(cumprod(a, axis=1), target_language='cpp'))
        assert np.allclose(
            np.cumprod(a, axis=2),
            ovl.evaluate(cumprod(a, axis=2), target_language='cpp'))

        if ovl.cuda_enabled:
            ovl.logger.debug(u'Testing CUDA')
            assert np.allclose(
                np.cumsum(a, axis=0),
                ovl.evaluate(cumsum(a, axis=0), target_language='cuda'))
            assert np.allclose(
                np.cumsum(a, axis=1),
                ovl.evaluate(cumsum(a, axis=1), target_language='cuda'))
            assert np.allclose(
                np.cumsum(a, axis=2),
                ovl.evaluate(cumsum(a, axis=2), target_language='cuda'))

            assert np.allclose(
                np.cumprod(a, axis=0),
                ovl.evaluate(cumprod(a, axis=0), target_language='cuda'))
            assert np.allclose(
                np.cumprod(a, axis=1),
                ovl.evaluate(cumprod(a, axis=1), target_language='cuda'))
            assert np.allclose(
                np.cumprod(a, axis=2),
                ovl.evaluate(cumprod(a, axis=2), target_language='cuda'))
    def test(self):
        """
        Test the outputs of the operators to make sure they are consistent with the numpy implementation
        """

        a = np.random.random((5, 5, 5))
        ovl.logger.debug(u'Testing C')
        assert np.allclose(np.cumsum(a, axis=0), ovl.evaluate(cumsum(a, axis=0), target_language='cpp'))
        assert np.allclose(np.cumsum(a, axis=1), ovl.evaluate(cumsum(a, axis=1), target_language='cpp'))
        assert np.allclose(np.cumsum(a, axis=2), ovl.evaluate(cumsum(a, axis=2), target_language='cpp'))

        assert np.allclose(np.cumprod(a, axis=0), ovl.evaluate(cumprod(a, axis=0), target_language='cpp'))
        assert np.allclose(np.cumprod(a, axis=1), ovl.evaluate(cumprod(a, axis=1), target_language='cpp'))
        assert np.allclose(np.cumprod(a, axis=2), ovl.evaluate(cumprod(a, axis=2), target_language='cpp'))

        if ovl.cuda_enabled:
            ovl.logger.debug(u'Testing CUDA')
            assert np.allclose(np.cumsum(a, axis=0), ovl.evaluate(cumsum(a, axis=0), target_language='cuda'))
            assert np.allclose(np.cumsum(a, axis=1), ovl.evaluate(cumsum(a, axis=1), target_language='cuda'))
            assert np.allclose(np.cumsum(a, axis=2), ovl.evaluate(cumsum(a, axis=2), target_language='cuda'))

            assert np.allclose(np.cumprod(a, axis=0), ovl.evaluate(cumprod(a, axis=0), target_language='cuda'))
            assert np.allclose(np.cumprod(a, axis=1), ovl.evaluate(cumprod(a, axis=1), target_language='cuda'))
            assert np.allclose(np.cumprod(a, axis=2), ovl.evaluate(cumprod(a, axis=2), target_language='cuda'))