예제 #1
0
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_mesh) {
    //! [ex_matrix_manipulation_mesh]
    float hx[] = {1, 2, 3, 4};
    float hy[] = {5, 6};

    array x = array(4, hx);
    array y = array(2, hy);

    af_print(tile(x, 1, 2));
    af_print(tile(y.T(), 4, 1));
    //! [ex_matrix_manipulation_mesh]

    array outx = tile(x, 1, 2);
    array outy = tile(y.T(), 4, 1);

    ASSERT_EQ(4, outx.dims(0));
    ASSERT_EQ(4, outy.dims(0));
    ASSERT_EQ(2, outx.dims(1));
    ASSERT_EQ(2, outy.dims(1));

    vector<float> houtx(outx.elements());
    outx.host(&houtx.front());
    vector<float> houty(outy.elements());
    outy.host(&houty.front());

    for (unsigned i = 0; i < houtx.size(); i++)
        ASSERT_EQ(hx[i % 4], houtx[i]) << "At [" << i << "]";
    for (unsigned i = 0; i < houty.size(); i++)
        ASSERT_EQ(hy[i > 3], houty[i]) << "At [" << i << "]";
}
예제 #2
0
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_tile) {
    //! [ex_matrix_manipulation_tile]
    float h[]       = {1, 2, 3, 4};
    array small_arr = array(2, 2, h);  // 2x2 matrix
    af_print(small_arr);
    array large_arr =
        tile(small_arr, 2, 3);  // produces 4x6 matrix: (2*2)x(2*3)
    af_print(large_arr);
    //! [ex_matrix_manipulation_tile]

    ASSERT_EQ(4, large_arr.dims(0));
    ASSERT_EQ(6, large_arr.dims(1));

    vector<float> h_large_arr(large_arr.elements());
    large_arr.host(&h_large_arr.front());

    unsigned fdim = large_arr.dims(0);
    unsigned sdim = large_arr.dims(1);
    for (unsigned i = 0; i < sdim; i++) {
        for (unsigned j = 0; j < fdim; j++) {
            ASSERT_FLOAT_EQ(h[(i % 2) * 2 + (j % 2)],
                            h_large_arr[i * fdim + j]);
        }
    }
}