Beispiel #1
0
Array<T>::Array(dim4 dims, const T * const in_data):
    ArrayInfo(getActiveDeviceId(), dims, dim4(0,0,0,0), calcStrides(dims), (af_dtype)dtype_traits<T>::af_type),
    data(memAlloc<T>(dims.elements()), memFree<T>), data_dims(dims),
    node(), ready(true), offset(0), owner(true)
{
    std::copy(in_data, in_data + dims.elements(), data.get());
}
Array<T> convolve2(Array<T> const& signal, Array<accT> const& c_filter, Array<accT> const& r_filter)
{
    const dim4 cfDims   = c_filter.dims();
    const dim4 rfDims   = r_filter.dims();

    const dim_t cfLen= cfDims.elements();
    const dim_t rfLen= rfDims.elements();

    const dim4 sDims = signal.dims();
    dim4 tDims = sDims;
    dim4 oDims = sDims;

    if (expand) {
        tDims[0] += cfLen - 1;
        oDims[0] += cfLen - 1;
        oDims[1] += rfLen - 1;
    }

    Array<T> temp= createEmptyArray<T>(tDims);
    Array<T> out = createEmptyArray<T>(oDims);

    kernel::convolve2<T, accT, 0, expand>(temp, signal, c_filter);
    kernel::convolve2<T, accT, 1, expand>(out, temp, r_filter);

    return out;
}
Beispiel #3
0
Array<T>::Array(dim4 dims, const T * const in_data, bool is_device, bool copy_device):
    info(getActiveDeviceId(), dims, 0, calcStrides(dims), (af_dtype)dtype_traits<T>::af_type),
    data((is_device & !copy_device) ? (T*)in_data : memAlloc<T>(dims.elements()).release(), memFree<T>), data_dims(dims),
    node(bufferNodePtr<T>()), ready(true), owner(true)
{
    static_assert(is_standard_layout<Array<T>>::value, "Array<T> must be a standard layout type");
    static_assert(offsetof(Array<T>, info) == 0, "Array<T>::info must be the first member variable of Array<T>");
    if (!is_device || copy_device) {
        // Ensure the memory being written to isnt used anywhere else.
        getQueue().sync();
        copy(in_data, in_data + dims.elements(), data.get());
    }
}
Beispiel #4
0
Array<T>::Array(dim4 dims)
    : info(getActiveDeviceId(), dims, 0, calcStrides(dims),
           (af_dtype)dtype_traits<T>::af_type)
    , data(memAlloc<T>(dims.elements()).release(), memFree<T>)
    , data_dims(dims)
    , node(bufferNodePtr<T>())
    , ready(true)
    , owner(true) {}
Beispiel #5
0
af_err af_approx1_uniform(af_array *yo, const af_array yi,
                          const af_array xo, const int xdim,
                          const double xi_beg, const double xi_step,
                          const af_interp_type method, const float offGrid)
{
    try {
        const ArrayInfo& yi_info = getInfo(yi);
        const ArrayInfo& xo_info = getInfo(xo);

        const dim4 yi_dims = yi_info.dims();
        const dim4 xo_dims = xo_info.dims();

        ARG_ASSERT(1, yi_info.isFloating());                        // Only floating and complex types
        ARG_ASSERT(2, xo_info.isRealFloating()) ;                   // Only floating types
        ARG_ASSERT(1, yi_info.isSingle() == xo_info.isSingle());    // Must have same precision
        ARG_ASSERT(1, yi_info.isDouble() == xo_info.isDouble());    // Must have same precision
        ARG_ASSERT(3, xdim >= 0 && xdim < 4);

        // POS should either be (x, 1, 1, 1) or (1, yi_dims[1], yi_dims[2], yi_dims[3])
        if (xo_dims[xdim] != xo_dims.elements()) {
            for (int i = 0; i < 4; i++) {
                if (xdim != i) DIM_ASSERT(2, xo_dims[i] == yi_dims[i]);
            }
        }

        ARG_ASSERT(5, xi_step != 0);
        ARG_ASSERT(6, (method == AF_INTERP_CUBIC         ||
                       method == AF_INTERP_CUBIC_SPLINE  ||
                       method == AF_INTERP_LINEAR        ||
                       method == AF_INTERP_LINEAR_COSINE ||
                       method == AF_INTERP_LOWER         ||
                       method == AF_INTERP_NEAREST));

        if (yi_dims.ndims() == 0 || xo_dims.ndims() ==  0) {
            *yo = createHandle(dim4(0,0,0,0), yi_info.getType());
            return AF_SUCCESS;
        }

        dim4 yo_dims = yi_dims;
        yo_dims[xdim] = xo_dims[xdim];
        if (*yo == 0) {
            *yo = createHandle(yo_dims, yi_info.getType());
        }

        DIM_ASSERT(1, getInfo(*yo).dims() == yo_dims);

        switch(yi_info.getType()) {
        case f32: approx1<float  , float >(yo, yi, xo, xdim,
                                           xi_beg, xi_step,
                                           method, offGrid);  break;
        case f64: approx1<double , double>(yo, yi, xo, xdim,
                                           xi_beg, xi_step,
                                           method, offGrid);  break;
        case c32: approx1<cfloat , float >(yo, yi, xo, xdim,
                                           xi_beg, xi_step,
                                           method, offGrid);  break;
        case c64: approx1<cdouble, double>(yo, yi, xo, xdim,
                                                    xi_beg, xi_step,
                                                    method, offGrid);  break;
        default:  TYPE_ERROR(1, yi_info.getType());
        }
    }
    CATCHALL;

    return AF_SUCCESS;
}
    c.host(&hc[0]);

    for (int i = 0; i < num; i++) {
        EXPECT_NEAR(hc[i], hb[i], 1e-7) << "at " << i;
    }
}

TEST(Select, 4D)
{
    dim4 dims(2, 3, 4, 2);
    array cond = randu(dims) > 0.5;
    array a = randu(dims);
    array b = select(cond, a - a * 0.9, a);
    array c = a - a * cond * 0.9;

    int num = (int)dims.elements();
    vector<float> hb(num);
    vector<float> hc(num);

    b.host(&hb[0]);
    c.host(&hc[0]);

    for (int i = 0; i < num; i++) {
        EXPECT_NEAR(hc[i], hb[i], 1e-7) << "at " << i;
    }
}

TEST(Select, Issue_1730)
{
    const int n = 1000;
    const int m = 200;