예제 #1
0
af_err af_medfilt(af_array *out, const af_array in, const dim_t wind_length, const dim_t wind_width, const af_border_type edge_pad)
{
    try {
        ARG_ASSERT(2, (wind_length==wind_width));
        ARG_ASSERT(2, (wind_length>0));
        ARG_ASSERT(3, (wind_width>0));
        ARG_ASSERT(4, (edge_pad>=AF_PAD_ZERO && edge_pad<=AF_PAD_SYM));

        ArrayInfo info = getInfo(in);
        af::dim4 dims  = info.dims();

        dim_t input_ndims = dims.ndims();
        DIM_ASSERT(1, (input_ndims >= 2));

        if (wind_length==1) {
            *out = retain(in);
        } else {
            af_array output;
            af_dtype type  = info.getType();
            switch(type) {
            case f32:
                output = medfilt<float >(in, wind_length, wind_width, edge_pad);
                break;
            case f64:
                output = medfilt<double>(in, wind_length, wind_width, edge_pad);
                break;
            case b8 :
                output = medfilt<char  >(in, wind_length, wind_width, edge_pad);
                break;
            case s32:
                output = medfilt<int   >(in, wind_length, wind_width, edge_pad);
                break;
            case u32:
                output = medfilt<uint  >(in, wind_length, wind_width, edge_pad);
                break;
            case u8 :
                output = medfilt<uchar >(in, wind_length, wind_width, edge_pad);
                break;
            default :
                TYPE_ERROR(1, type);
            }
            std::swap(*out, output);
        }
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #2
0
af_err af_assign(af_array *out,
                 const af_array lhs, const unsigned ndims,
                 const af_seq *index, const af_array rhs)
{
    try {
        ARG_ASSERT(0, (lhs!=0));
        ARG_ASSERT(1, (ndims>0));
        ARG_ASSERT(3, (rhs!=0));

        for(dim_type i=0; i<(dim_type)ndims; ++i) {
            ARG_ASSERT(2, (index[i].step>=0));
        }

        af_array res;
        if (*out != lhs) AF_CHECK(af_copy_array(&res, lhs));
        else             res = weakCopy(lhs);

        try {

            if (lhs != rhs) {
                ArrayInfo oInfo = getInfo(lhs);
                af_dtype oType  = oInfo.getType();
                switch(oType) {
                case c64: assign<cdouble, true >(res, ndims, index, rhs);  break;
                case c32: assign<cfloat , true >(res, ndims, index, rhs);  break;
                case f64: assign<double , false>(res, ndims, index, rhs);  break;
                case f32: assign<float  , false>(res, ndims, index, rhs);  break;
                case s32: assign<int    , false>(res, ndims, index, rhs);  break;
                case u32: assign<uint   , false>(res, ndims, index, rhs);  break;
                case u8 : assign<uchar  , false>(res, ndims, index, rhs);  break;
                case b8 : assign<char   , false>(res, ndims, index, rhs);  break;
                default : TYPE_ERROR(1, oType); break;
                }
            }

        } catch(...) {
            if (*out != lhs) {
                AF_CHECK(af_destroy_array(res));
            }
            throw;
        }
        std::swap(*out, res);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #3
0
af_err af_rotate(af_array *out, const af_array in, const float theta,
                 const bool crop,
                 const af_interp_type method)
{
    try {
        unsigned odims0 = 0, odims1 = 0;

        ArrayInfo info = getInfo(in);
        af::dim4 idims = info.dims();

        if(!crop) {
            odims0 = idims[0] * fabs(std::cos(theta)) + idims[1] * fabs(std::sin(theta));
            odims1 = idims[1] * fabs(std::cos(theta)) + idims[0] * fabs(std::sin(theta));
        } else {
            odims0 = idims[0];
            odims1 = idims[1];
        }

        af_dtype itype = info.getType();

        ARG_ASSERT(3, method == AF_INTERP_NEAREST  ||
                      method == AF_INTERP_BILINEAR ||
                      method == AF_INTERP_LOWER);

        DIM_ASSERT(1, idims.elements() > 0);

        af::dim4 odims(odims0, odims1, idims[2], idims[3]);

        af_array output = 0;
        switch(itype) {
            case f32: output = rotate<float  >(in, theta, odims, method);  break;
            case f64: output = rotate<double >(in, theta, odims, method);  break;
            case c32: output = rotate<cfloat >(in, theta, odims, method);  break;
            case c64: output = rotate<cdouble>(in, theta, odims, method);  break;
            case s32: output = rotate<int    >(in, theta, odims, method);  break;
            case u32: output = rotate<uint   >(in, theta, odims, method);  break;
            case s64: output = rotate<intl   >(in, theta, odims, method);  break;
            case u64: output = rotate<uintl  >(in, theta, odims, method);  break;
            case u8:  output = rotate<uchar  >(in, theta, odims, method);  break;
            case b8:  output = rotate<uchar  >(in, theta, odims, method);  break;
            default:  TYPE_ERROR(1, itype);
        }
        std::swap(*out,output);
    } CATCHALL

    return AF_SUCCESS;
}
예제 #4
0
af_err af_assign_seq(af_array *out,
                     const af_array lhs, const unsigned ndims,
                     const af_seq *index, const af_array rhs)
{
    try {
        ARG_ASSERT(0, (lhs!=0));
        ARG_ASSERT(1, (ndims>0));
        ARG_ASSERT(3, (rhs!=0));

        for(dim_t i=0; i<(dim_t)ndims; ++i) {
            ARG_ASSERT(2, (index[i].step>=0));
        }

        af_array res;
        if (*out != lhs) AF_CHECK(af_copy_array(&res, lhs));
        else             res = retain(lhs);

        try {

            if (lhs != rhs) {
                ArrayInfo oInfo = getInfo(lhs);
                af_dtype oType  = oInfo.getType();
                switch(oType) {
                case c64: assign_helper<cdouble>(getWritableArray<cdouble>(res), ndims, index, rhs);  break;
                case c32: assign_helper<cfloat >(getWritableArray<cfloat >(res), ndims, index, rhs);  break;
                case f64: assign_helper<double >(getWritableArray<double >(res), ndims, index, rhs);  break;
                case f32: assign_helper<float  >(getWritableArray<float  >(res), ndims, index, rhs);  break;
                case s32: assign_helper<int    >(getWritableArray<int    >(res), ndims, index, rhs);  break;
                case u32: assign_helper<uint   >(getWritableArray<uint   >(res), ndims, index, rhs);  break;
                case s64: assign_helper<intl   >(getWritableArray<intl   >(res), ndims, index, rhs);  break;
                case u64: assign_helper<uintl  >(getWritableArray<uintl  >(res), ndims, index, rhs);  break;
                case u8 : assign_helper<uchar  >(getWritableArray<uchar  >(res), ndims, index, rhs);  break;
                case b8 : assign_helper<char   >(getWritableArray<char   >(res), ndims, index, rhs);  break;
                default : TYPE_ERROR(1, oType); break;
                }
            }
        } catch(...) {
            af_release_array(res);
            throw;
        }
        std::swap(*out, res);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #5
0
af_err af_histogram(af_array *out, const af_array in,
                    const unsigned nbins, const double minval, const double maxval)
{
    try {
        ArrayInfo info = getInfo(in);
        af_dtype type  = info.getType();

        af_array output;
        switch(type) {
            case f32: output = histogram<float , uint>(in, nbins, minval, maxval, info.isLinear()); break;
            case f64: output = histogram<double, uint>(in, nbins, minval, maxval, info.isLinear()); break;
            case b8 : output = histogram<char  , uint>(in, nbins, minval, maxval, info.isLinear()); break;
            case s32: output = histogram<int   , uint>(in, nbins, minval, maxval, info.isLinear()); break;
            case u32: output = histogram<uint  , uint>(in, nbins, minval, maxval, info.isLinear()); break;
            case s16: output = histogram<short , uint>(in, nbins, minval, maxval, info.isLinear()); break;
            case u16: output = histogram<ushort, uint>(in, nbins, minval, maxval, info.isLinear()); break;
            case s64: output = histogram<intl  , uint>(in, nbins, minval, maxval, info.isLinear()); break;
            case u64: output = histogram<uintl , uint>(in, nbins, minval, maxval, info.isLinear()); break;
            case u8 : output = histogram<uchar , uint>(in, nbins, minval, maxval, info.isLinear()); break;
            default : TYPE_ERROR(1, type);
        }
        std::swap(*out,output);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #6
0
static void print(af_array arr)
{
    const ArrayInfo info = getInfo(arr);
    T *data = new T[info.elements()];

    af_array arrT;
    AF_CHECK(af_reorder(&arrT, arr, 1, 0, 2, 3));

    //FIXME: Use alternative function to avoid copies if possible
    AF_CHECK(af_get_data_ptr(data, arrT));
    const ArrayInfo infoT = getInfo(arrT);
    AF_CHECK(af_destroy_array(arrT));

    std::ios_base::fmtflags backup = std::cout.flags();

    std::cout << "[" << info.dims() << "]\n";
#ifndef NDEBUG
    std::cout <<"   Offsets: ["<<info.offsets()<<"]"<<std::endl;
    std::cout <<"   Strides: ["<<info.strides()<<"]"<<std::endl;
#endif

    printer(std::cout, data, infoT, infoT.ndims() - 1);

    delete[] data;

    std::cout.flags(backup);
}
예제 #7
0
void sort_by_key_tmplt(af_array *okey, af_array *oval, const af_array ikey, const af_array ival,
                       const unsigned dim, const bool isAscending)
{
    ArrayInfo info = getInfo(ival);
    af_dtype vtype = info.getType();

    switch(vtype) {
    case f32: sort_by_key<Tk, float  >(okey, oval, ikey, ival, dim, isAscending);  break;
    case f64: sort_by_key<Tk, double >(okey, oval, ikey, ival, dim, isAscending);  break;
    case s32: sort_by_key<Tk, int    >(okey, oval, ikey, ival, dim, isAscending);  break;
    case u32: sort_by_key<Tk, uint   >(okey, oval, ikey, ival, dim, isAscending);  break;
    case u8:  sort_by_key<Tk, uchar  >(okey, oval, ikey, ival, dim, isAscending);  break;
    case b8:  sort_by_key<Tk, char   >(okey, oval, ikey, ival, dim, isAscending);  break;
    default:  TYPE_ERROR(1, vtype);
    }

    return;
}
예제 #8
0
static af_array lookup(const af_array &in, const af_array &idx, const unsigned dim)
{
    ArrayInfo inInfo = getInfo(in);

    af_dtype inType  = inInfo.getType();

    switch(inType) {
        case f32: return getHandle(lookup<float   , idx_t > (getArray<float   >(in), getArray<idx_t>(idx), dim));
        case c32: return getHandle(lookup<cfloat  , idx_t > (getArray<cfloat  >(in), getArray<idx_t>(idx), dim));
        case f64: return getHandle(lookup<double  , idx_t > (getArray<double  >(in), getArray<idx_t>(idx), dim));
        case c64: return getHandle(lookup<cdouble , idx_t > (getArray<cdouble >(in), getArray<idx_t>(idx), dim));
        case s32: return getHandle(lookup<int     , idx_t > (getArray<int     >(in), getArray<idx_t>(idx), dim));
        case u32: return getHandle(lookup<unsigned, idx_t > (getArray<unsigned>(in), getArray<idx_t>(idx), dim));
        case  u8: return getHandle(lookup<uchar   , idx_t > (getArray<uchar   >(in), getArray<idx_t>(idx), dim));
        case  b8: return getHandle(lookup<char    , idx_t > (getArray<char    >(in), getArray<idx_t>(idx), dim));
        default : TYPE_ERROR(1, inType);
    }
}
예제 #9
0
af_err af_print_array(af_array arr)
{
    try {
        ArrayInfo info = getInfo(arr);
        af_dtype type = info.getType();
        switch(type)
        {
        case f32:
            print<float>(arr);
            break;
        case c32:
            print<cfloat>(arr);
            break;
        case f64:
            print<double>(arr);
            break;
        case c64:
            print<cdouble>(arr);
            break;
        case b8:
            print<char>(arr);
            break;
        case s32:
            print<int>(arr);
            break;
        case u32:
            print<unsigned>(arr);
            break;
        case u8:
            print<uchar>(arr);
            break;
        case s64:
            print<intl>(arr);
            break;
        case u64:
            print<uintl>(arr);
            break;
        default:
            TYPE_ERROR(1, type);
        }
    }
    CATCHALL;
    return AF_SUCCESS;
}
예제 #10
0
파일: array.cpp 프로젝트: victorv/arrayfire
af_err af_release_array(af_array arr)
{
    try {
        int dev = getActiveDeviceId();

        ArrayInfo info = getInfo(arr, false, false);
        af_dtype type = info.getType();

        if(info.isSparse()) {
            switch(type) {
                case f32: releaseSparseHandle<float  >(arr); break;
                case f64: releaseSparseHandle<double >(arr); break;
                case c32: releaseSparseHandle<cfloat >(arr); break;
                case c64: releaseSparseHandle<cdouble>(arr); break;
                default : TYPE_ERROR(0, type);
            }
        } else {

            setDevice(info.getDevId());

            switch(type) {
            case f32:   releaseHandle<float   >(arr); break;
            case c32:   releaseHandle<cfloat  >(arr); break;
            case f64:   releaseHandle<double  >(arr); break;
            case c64:   releaseHandle<cdouble >(arr); break;
            case b8:    releaseHandle<char    >(arr); break;
            case s32:   releaseHandle<int     >(arr); break;
            case u32:   releaseHandle<uint    >(arr); break;
            case u8:    releaseHandle<uchar   >(arr); break;
            case s64:   releaseHandle<intl    >(arr); break;
            case u64:   releaseHandle<uintl   >(arr); break;
            case s16:   releaseHandle<short   >(arr); break;
            case u16:   releaseHandle<ushort  >(arr); break;
            default:    TYPE_ERROR(0, type);
            }

            setDevice(dev);
        }
    }
    CATCHALL

    return AF_SUCCESS;
}
예제 #11
0
af_err af_reorder(af_array *out, const af_array in, const af::dim4 &rdims)
{
    try {
        ArrayInfo info = getInfo(in);
        af_dtype type = info.getType();

        DIM_ASSERT(1, info.elements() > 0);

        // Check that dimensions are not repeated
        int allDims[] = {0, 1, 2, 3};
        for(int i = 0; i < 3; i++) {
            DIM_ASSERT(i + 2, rdims[i] == allDims[rdims[i]]);
            allDims[rdims[i]] = -1;
        }

        // If reorder is a (batched) transpose, then call transpose
        if(info.dims()[3] == 1) {
            if(rdims[0] == 1 && rdims[1] == 0 &&
               rdims[2] == 2 && rdims[3] == 3) {
                return af_transpose(out, in);
            }
        }

        af_array output;

        switch(type) {
            case f32: output = reorder<float  >(in, rdims);  break;
            case c32: output = reorder<cfloat >(in, rdims);  break;
            case f64: output = reorder<double >(in, rdims);  break;
            case c64: output = reorder<cdouble>(in, rdims);  break;
            case b8:  output = reorder<char   >(in, rdims);  break;
            case s32: output = reorder<int    >(in, rdims);  break;
            case u32: output = reorder<uint   >(in, rdims);  break;
            case u8:  output = reorder<uchar  >(in, rdims);  break;
            case s8:  output = reorder<char   >(in, rdims);  break;
            default:  TYPE_ERROR(1, type);
        }
        std::swap(*out,output);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #12
0
파일: blas.cpp 프로젝트: victorv/arrayfire
af_err af_dot(      af_array *out,
                    const af_array lhs, const af_array rhs,
                    const af_mat_prop optLhs, const af_mat_prop optRhs)
{
    using namespace detail;

    try {
        ArrayInfo lhsInfo = getInfo(lhs);
        ArrayInfo rhsInfo = getInfo(rhs);

        if (optLhs != AF_MAT_NONE && optLhs != AF_MAT_CONJ) {
            AF_ERROR("Using this property is not yet supported in dot", AF_ERR_NOT_SUPPORTED);
        }

        if (optRhs != AF_MAT_NONE && optRhs != AF_MAT_CONJ) {
            AF_ERROR("Using this property is not yet supported in dot", AF_ERR_NOT_SUPPORTED);
        }

        DIM_ASSERT(1, lhsInfo.dims()[0] == rhsInfo.dims()[0]);
        af_dtype lhs_type = lhsInfo.getType();
        af_dtype rhs_type = rhsInfo.getType();

        if(lhsInfo.ndims() == 0) {
            return af_retain_array(out, lhs);
        }
        if (lhsInfo.ndims() > 1 ||
            rhsInfo.ndims() > 1) {
            AF_ERROR("dot can not be used in batch mode", AF_ERR_BATCH);
        }

        TYPE_ASSERT(lhs_type == rhs_type);

        af_array output = 0;

        switch(lhs_type) {
        case f32: output = dot<float  >(lhs, rhs, optLhs, optRhs);    break;
        case c32: output = dot<cfloat >(lhs, rhs, optLhs, optRhs);    break;
        case f64: output = dot<double >(lhs, rhs, optLhs, optRhs);    break;
        case c64: output = dot<cdouble>(lhs, rhs, optLhs, optRhs);    break;
        default:  TYPE_ERROR(1, lhs_type);
        }
        std::swap(*out, output);
    }
    CATCHALL
        return AF_SUCCESS;
}
예제 #13
0
af_err af_matmul(   af_array *out,
                    const af_array lhs, const af_array rhs,
                    const af_mat_prop optLhs, const af_mat_prop optRhs)
{
    using namespace detail;

    try {
        ArrayInfo lhsInfo = getInfo(lhs);
        ArrayInfo rhsInfo = getInfo(rhs);

        af_dtype lhs_type = lhsInfo.getType();
        af_dtype rhs_type = rhsInfo.getType();

        if (!(optLhs == AF_MAT_NONE ||
              optLhs == AF_MAT_TRANS ||
              optLhs == AF_MAT_CTRANS)) {
            AF_ERROR("Using this property is not yet supported in matmul", AF_ERR_NOT_SUPPORTED);
        }

        if (!(optRhs == AF_MAT_NONE ||
              optRhs == AF_MAT_TRANS ||
              optRhs == AF_MAT_CTRANS)) {
            AF_ERROR("Using this property is not yet supported in matmul", AF_ERR_NOT_SUPPORTED);
        }


        if (lhsInfo.ndims() > 2 ||
            rhsInfo.ndims() > 2) {
            AF_ERROR("matmul can not be used in batch mode", AF_ERR_BATCH);
        }

        TYPE_ASSERT(lhs_type == rhs_type);
        af_array output = 0;

        int aColDim = (optLhs == AF_MAT_NONE) ? 1 : 0;
        int bRowDim = (optRhs == AF_MAT_NONE) ? 0 : 1;

        DIM_ASSERT(1, lhsInfo.dims()[aColDim] == rhsInfo.dims()[bRowDim]);

        switch(lhs_type) {
        case f32: output = matmul<float  >(lhs, rhs, optLhs, optRhs);   break;
        case c32: output = matmul<cfloat >(lhs, rhs, optLhs, optRhs);   break;
        case f64: output = matmul<double >(lhs, rhs, optLhs, optRhs);   break;
        case c64: output = matmul<cdouble>(lhs, rhs, optLhs, optRhs);   break;
        default:  TYPE_ERROR(1, lhs_type);
        }
        std::swap(*out, output);
    }
    CATCHALL
    return AF_SUCCESS;
}
예제 #14
0
파일: sort.cpp 프로젝트: EasonYi/arrayfire
af_err sort_by_key_tmplt(af_array *okey, af_array *oval, const af_array ikey, const af_array ival,
                         const unsigned dim, const bool dir)
{
    try {
        ArrayInfo info = getInfo(ival);
        af_dtype vtype = info.getType();

        switch(vtype) {
            case f32: sort_by_key<Tk, float  >(okey, oval, ikey, ival, dim, dir);  break;
            case f64: sort_by_key<Tk, double >(okey, oval, ikey, ival, dim, dir);  break;
            case s32: sort_by_key<Tk, int    >(okey, oval, ikey, ival, dim, dir);  break;
            case u32: sort_by_key<Tk, uint   >(okey, oval, ikey, ival, dim, dir);  break;
            case u8:  sort_by_key<Tk, uchar  >(okey, oval, ikey, ival, dim, dir);  break;
         // case s8:  sort_by_key<Tk, char   >(okey, oval, ikey, ival, dim, dir);  break;
            default:  TYPE_ERROR(1, vtype);
        }
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #15
0
af_err af_sort_by_key(af_array *out_keys, af_array *out_values,
                      const af_array keys, const af_array values,
                      const unsigned dim, const bool isAscending)
{
    try {
        ArrayInfo info = getInfo(keys);
        af_dtype type = info.getType();

        ArrayInfo vinfo = getInfo(values);

        DIM_ASSERT(3, info.elements() > 0);
        DIM_ASSERT(4, info.dims() == vinfo.dims());
        // Only Dim 0 supported
        ARG_ASSERT(5, dim == 0);

        af_array oKey;
        af_array oVal;

        switch(type) {
            case f32: sort_by_key_tmplt<float  >(&oKey, &oVal, keys, values, dim, isAscending);  break;
            case f64: sort_by_key_tmplt<double >(&oKey, &oVal, keys, values, dim, isAscending);  break;
            case s32: sort_by_key_tmplt<int    >(&oKey, &oVal, keys, values, dim, isAscending);  break;
            case u32: sort_by_key_tmplt<uint   >(&oKey, &oVal, keys, values, dim, isAscending);  break;
            case u8:  sort_by_key_tmplt<uchar  >(&oKey, &oVal, keys, values, dim, isAscending);  break;
            case b8:  sort_by_key_tmplt<char   >(&oKey, &oVal, keys, values, dim, isAscending);  break;
            default:  TYPE_ERROR(1, type);
        }
        std::swap(*out_keys , oKey);
        std::swap(*out_values , oVal);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #16
0
fg::Plot3* setup_plot3(const af_array P, fg::PlotType ptype, fg::MarkerType mtype)
{
    Array<T> pIn = getArray<T>(P);
    ArrayInfo Pinfo = getInfo(P);
    af::dim4 P_dims = Pinfo.dims();

    DIM_ASSERT(0, Pinfo.ndims() == 1 || Pinfo.ndims() == 2);
    DIM_ASSERT(0, (P_dims[0] == 3 || P_dims[1] == 3) ||
                    (Pinfo.isVector() && P_dims[0]%3 == 0));

    if(Pinfo.isVector()){
        dim4 rdims(P_dims.elements()/3, 3, 1, 1);
        pIn.modDims(rdims);
        P_dims = pIn.dims();
    }

    if(P_dims[1] == 3){
        pIn = transpose(pIn, false);
    }

    T max[3], min[3];
    copyData(max, reduce<af_max_t, T, T>(pIn, 1));
    copyData(min, reduce<af_min_t, T, T>(pIn, 1));

    ForgeManager& fgMngr = ForgeManager::getInstance();
    fg::Plot3* plot3 = fgMngr.getPlot3(P_dims.elements()/3, getGLType<T>(), ptype, mtype);
    plot3->setColor(1.0, 0.0, 0.0);
    plot3->setAxesLimits(max[0], min[0],
                         max[1], min[1],
                         max[2], min[2]);
    plot3->setAxesTitles("X Axis", "Y Axis", "Z Axis");
    copy_plot3<T>(pIn, plot3);
    return plot3;
}
예제 #17
0
af_err af_corrcoef(double *realVal, double *imagVal, const af_array X, const af_array Y)
{
    try {
        ArrayInfo xInfo = getInfo(X);
        ArrayInfo yInfo = getInfo(Y);
        dim4 xDims      = xInfo.dims();
        dim4 yDims      = yInfo.dims();
        af_dtype xType  = xInfo.getType();
        af_dtype yType  = yInfo.getType();

        ARG_ASSERT(2, (xType==yType));
        ARG_ASSERT(2, (xDims.ndims()==yDims.ndims()));

        for (dim_t i=0; i<xDims.ndims(); ++i)
            ARG_ASSERT(2, (xDims[i]==yDims[i]));

        switch(xType) {
            case f64: *realVal = corrcoef<double, double>(X, Y); break;
            case f32: *realVal = corrcoef<float , float >(X, Y); break;
            case s32: *realVal = corrcoef<int   , float >(X, Y); break;
            case u32: *realVal = corrcoef<uint  , float >(X, Y); break;
            case s64: *realVal = corrcoef<intl  , double>(X, Y); break;
            case u64: *realVal = corrcoef<uintl , double>(X, Y); break;
            case s16: *realVal = corrcoef<short , float >(X, Y); break;
            case u16: *realVal = corrcoef<ushort, float >(X, Y); break;
            case  u8: *realVal = corrcoef<uchar , float >(X, Y); break;
            case  b8: *realVal = corrcoef<char  , float >(X, Y); break;
            default : TYPE_ERROR(1, xType);
        }
    }
    CATCHALL;
    return AF_SUCCESS;
}
예제 #18
0
파일: hist.cpp 프로젝트: JasonZen/arrayfire
af_err af_draw_hist(const af_window wind, const af_array X, const double minval, const double maxval,
                    const af_cell* const props)
{
#if defined(WITH_GRAPHICS)
    if(wind==0) {
        std::cerr<<"Not a valid window"<<std::endl;
        return AF_SUCCESS;
    }

    try {
        ArrayInfo Xinfo = getInfo(X);
        af_dtype Xtype  = Xinfo.getType();

        ARG_ASSERT(0, Xinfo.isVector());

        fg::Window* window = reinterpret_cast<fg::Window*>(wind);
        window->makeCurrent();
        fg::Histogram* hist = NULL;

        switch(Xtype) {
            case f32: hist = setup_histogram<float  >(X, minval, maxval); break;
            case s32: hist = setup_histogram<int    >(X, minval, maxval); break;
            case u32: hist = setup_histogram<uint   >(X, minval, maxval); break;
            case s16: hist = setup_histogram<short  >(X, minval, maxval); break;
            case u16: hist = setup_histogram<ushort >(X, minval, maxval); break;
            case u8 : hist = setup_histogram<uchar  >(X, minval, maxval); break;
            default:  TYPE_ERROR(1, Xtype);
        }

        if (props->col>-1 && props->row>-1)
            window->draw(props->col, props->row, *hist, props->title);
        else
            window->draw(*hist);
    }
    CATCHALL;
    return AF_SUCCESS;
#else
    return AF_ERR_NO_GFX;
#endif
}
예제 #19
0
af_err af_gloh(af_features* feat, af_array* desc, const af_array in, const unsigned n_layers,
               const float contrast_thr, const float edge_thr, const float init_sigma,
               const bool double_input, const float img_scale, const float feature_ratio)
{
    try {
#ifdef AF_BUILD_NONFREE_SIFT
        ArrayInfo info = getInfo(in);
        af::dim4 dims  = info.dims();

        ARG_ASSERT(2, (dims[0] >= 15 && dims[1] >= 15 && dims[2] == 1 && dims[3] == 1));
        ARG_ASSERT(3, n_layers > 0);
        ARG_ASSERT(4, contrast_thr > 0.0f);
        ARG_ASSERT(5, edge_thr >= 1.0f);
        ARG_ASSERT(6, init_sigma > 0.5f);
        ARG_ASSERT(8, img_scale > 0.0f);
        ARG_ASSERT(9, feature_ratio > 0.0f);

        dim_t in_ndims = dims.ndims();
        DIM_ASSERT(1, (in_ndims <= 3 && in_ndims >= 2));

        af_array tmp_desc;
        af_dtype type  = info.getType();
        switch(type) {
            case f32: sift<float , float >(*feat, tmp_desc, in, n_layers, contrast_thr,
                                           edge_thr, init_sigma, double_input,
                                           img_scale, feature_ratio, true); break;
            case f64: sift<double, double>(*feat, tmp_desc, in, n_layers, contrast_thr,
                                           edge_thr, init_sigma, double_input,
                                           img_scale, feature_ratio, true); break;
            default : TYPE_ERROR(1, type);
        }
        std::swap(*desc, tmp_desc);
#else
        AF_ERROR("ArrayFire was not built with nonfree support, GLOH disabled\n", AF_ERR_NONFREE);
#endif
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #20
0
파일: binary.cpp 프로젝트: DeepCV/arrayfire
static af_err af_arith_real(af_array *out, const af_array lhs, const af_array rhs, const bool batchMode)
{
    try {

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        const af_dtype otype = implicit(linfo.getType(), rinfo.getType());
        af_array res;
        switch (otype) {
        case f32: res = arithOp<float  , op>(lhs, rhs, odims); break;
        case f64: res = arithOp<double , op>(lhs, rhs, odims); break;
        case s32: res = arithOp<int    , op>(lhs, rhs, odims); break;
        case u32: res = arithOp<uint   , op>(lhs, rhs, odims); break;
        case u8 : res = arithOp<uchar  , op>(lhs, rhs, odims); break;
        case b8 : res = arithOp<char   , op>(lhs, rhs, odims); break;
        case s64: res = arithOp<intl   , op>(lhs, rhs, odims); break;
        case u64: res = arithOp<uintl  , op>(lhs, rhs, odims); break;
        case s16: res = arithOp<short  , op>(lhs, rhs, odims); break;
        case u16: res = arithOp<ushort , op>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, otype);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}
예제 #21
0
af_err af_unwrap(af_array *out, const af_array in, const dim_t wx, const dim_t wy,
                 const dim_t sx, const dim_t sy, const dim_t px, const dim_t py, const bool is_column)
{
    try {
        ArrayInfo info = getInfo(in);
        af_dtype type = info.getType();
        af::dim4 idims = info.dims();

        ARG_ASSERT(2, wx > 0 && wx <= idims[0] + px);
        ARG_ASSERT(3, wy > 0 && wy <= idims[1] + py);
        ARG_ASSERT(4, sx > 0);
        ARG_ASSERT(5, sy > 0);
        ARG_ASSERT(6, px >= 0 && px < wx);
        ARG_ASSERT(7, py >= 0 && py < wy);

        af_array output;

        switch(type) {
            case f32: output = unwrap<float  >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case f64: output = unwrap<double >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case c32: output = unwrap<cfloat >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case c64: output = unwrap<cdouble>(in, wx, wy, sx, sy, px, py, is_column);  break;
            case s32: output = unwrap<int    >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case u32: output = unwrap<uint   >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case s64: output = unwrap<intl   >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case u64: output = unwrap<uintl  >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case s16: output = unwrap<short  >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case u16: output = unwrap<ushort >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case u8:  output = unwrap<uchar  >(in, wx, wy, sx, sy, px, py, is_column);  break;
            case b8:  output = unwrap<char   >(in, wx, wy, sx, sy, px, py, is_column);  break;
            default:  TYPE_ERROR(1, type);
        }
        std::swap(*out,output);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #22
0
bool isFreqDomain(const af_array &signal, const af_array filter, af_conv_domain domain)
{
    if (domain == AF_CONV_FREQ) return true;
    if (domain != AF_CONV_AUTO) return false;

    ArrayInfo sInfo = getInfo(signal);
    ArrayInfo fInfo = getInfo(filter);

    dim4 sdims = sInfo.dims();
    dim4 fdims = fInfo.dims();

    if (identifyBatchKind<baseDim>(sdims, fdims) == CONVOLVE_BATCH_DIFF) return true;

    int kbatch = 1;
    for(int i = 3; i >= baseDim; i--) {
        kbatch *= fdims[i];
    }

    if (kbatch >= 10) return true;

    if (baseDim == 1) {
        if (fdims[0] > 128) return true;
    }

    if (baseDim == 2) {
        // maximum supported size in 2D domain
        if (fdims[0] > 17 || fdims[1] > 17) return true;

        // Maximum supported non square size
        if (fdims[0] != fdims[1] && fdims[0] > 5) return true;
    }

    if (baseDim == 3) {
        if (fdims[0] > 5 || fdims[1] > 5 || fdims[2] > 5) return true;
    }

    return false;
}
예제 #23
0
static void printer(ostream &out, const T* ptr, const ArrayInfo &info, unsigned dim)
{

    dim_type stride =   info.strides()[dim];
    dim_type d      =   info.dims()[dim];
    ToNum<T> toNum;

    if(dim == 0) {
        for(dim_type i = 0, j = 0; i < d; i++, j+=stride) {
            out<<   std::fixed <<
                    std::setw(10) <<
                    std::setprecision(4) << toNum(ptr[j]) << " ";
        }
        out << endl;
    }
    else {
        for(dim_type i = 0; i < d; i++) {
            printer(out, ptr, info, dim - 1);
            ptr += stride;
        }
        out << endl;
    }
}
예제 #24
0
파일: index.cpp 프로젝트: hxiaox/arrayfire
af_err af_index(af_array *result, const af_array in, const unsigned ndims, const af_seq* index)
{
    af_array out;
    try {

        ArrayInfo iInfo = getInfo(in);
        if (ndims == 1 && ndims != (dim_t)iInfo.ndims()) {
            af_array tmp_in;
            AF_CHECK(af_flat(&tmp_in, in));
            AF_CHECK(af_index(result, tmp_in, ndims, index));
            AF_CHECK(af_release_array(tmp_in));
            return AF_SUCCESS;
        }

        af_dtype in_type = iInfo.getType();

        switch(in_type) {
        case f32:    indexArray<float>   (out, in, ndims, index);  break;
        case c32:    indexArray<cfloat>  (out, in, ndims, index);  break;
        case f64:    indexArray<double>  (out, in, ndims, index);  break;
        case c64:    indexArray<cdouble> (out, in, ndims, index);  break;
        case b8:     indexArray<char>    (out, in, ndims, index);  break;
        case s32:    indexArray<int>     (out, in, ndims, index);  break;
        case u32:    indexArray<unsigned>(out, in, ndims, index);  break;
        case s16:    indexArray<short>   (out, in, ndims, index);  break;
        case u16:    indexArray<ushort>  (out, in, ndims, index);  break;
        case s64:    indexArray<intl>    (out, in, ndims, index);  break;
        case u64:    indexArray<uintl>   (out, in, ndims, index);  break;
        case u8:     indexArray<uchar>   (out, in, ndims, index);  break;
        default:    TYPE_ERROR(1, in_type);
        }
    }
    CATCHALL

    swap(*result, out);
    return AF_SUCCESS;
}
예제 #25
0
af_err af_print_array_gen(const char *exp, const af_array arr, const int precision)
{
    try {
        ARG_ASSERT(0, exp != NULL);
        ArrayInfo info = getInfo(arr);
        af_dtype type = info.getType();
        switch(type)
        {
        case f32:   print<float   >(exp, arr, precision);   break;
        case c32:   print<cfloat  >(exp, arr, precision);   break;
        case f64:   print<double  >(exp, arr, precision);   break;
        case c64:   print<cdouble >(exp, arr, precision);   break;
        case b8:    print<char    >(exp, arr, precision);   break;
        case s32:   print<int     >(exp, arr, precision);   break;
        case u32:   print<unsigned>(exp, arr, precision);   break;
        case u8:    print<uchar   >(exp, arr, precision);   break;
        case s64:   print<intl    >(exp, arr, precision);   break;
        case u64:   print<uintl   >(exp, arr, precision);   break;
        default:    TYPE_ERROR(1, type);
        }
    }
    CATCHALL;
    return AF_SUCCESS;
}
예제 #26
0
파일: sort.cpp 프로젝트: victorv/arrayfire
af_err af_sort_index(af_array *out, af_array *indices, const af_array in, const unsigned dim, const bool isAscending)
{
    try {
        ArrayInfo info = getInfo(in);
        af_dtype type = info.getType();

        if(info.elements() <= 0) {
            dim_t my_dims[] = {0, 0, 0, 0};
            AF_CHECK(af_create_handle(out,     AF_MAX_DIMS, my_dims, type));
            AF_CHECK(af_create_handle(indices, AF_MAX_DIMS, my_dims, type));
            return AF_SUCCESS;
        }

        af_array val;
        af_array idx;

        switch(type) {
            case f32: sort_index<float  >(&val, &idx, in, dim, isAscending);  break;
            case f64: sort_index<double >(&val, &idx, in, dim, isAscending);  break;
            case s32: sort_index<int    >(&val, &idx, in, dim, isAscending);  break;
            case u32: sort_index<uint   >(&val, &idx, in, dim, isAscending);  break;
            case s16: sort_index<short  >(&val, &idx, in, dim, isAscending);  break;
            case u16: sort_index<ushort >(&val, &idx, in, dim, isAscending);  break;
            case s64: sort_index<intl   >(&val, &idx, in, dim, isAscending);  break;
            case u64: sort_index<uintl  >(&val, &idx, in, dim, isAscending);  break;
            case u8:  sort_index<uchar  >(&val, &idx, in, dim, isAscending);  break;
            case b8:  sort_index<char   >(&val, &idx, in, dim, isAscending);  break;
            default:  TYPE_ERROR(1, type);
        }
        std::swap(*out , val);
        std::swap(*indices, idx);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #27
0
static af_err reduce_common(af_array *out, const af_array in, const int dim)
{
    try {

        ARG_ASSERT(2, dim >= 0);
        ARG_ASSERT(2, dim <  4);

        const ArrayInfo in_info = getInfo(in);

        if (dim >= (int)in_info.ndims()) {
            *out = weakCopy(in);
            return AF_SUCCESS;
        }

        af_dtype type = in_info.getType();
        af_array res;

        switch(type) {
        case f32:  res = reduce<op, float  , float  >(in, dim); break;
        case f64:  res = reduce<op, double , double >(in, dim); break;
        case c32:  res = reduce<op, cfloat , cfloat >(in, dim); break;
        case c64:  res = reduce<op, cdouble, cdouble>(in, dim); break;
        case u32:  res = reduce<op, uint   , uint   >(in, dim); break;
        case s32:  res = reduce<op, int    , int    >(in, dim); break;
        case b8:   res = reduce<op, uchar  , uchar  >(in, dim); break;
        case u8:   res = reduce<op, uchar  , uchar  >(in, dim); break;
        case s8:   res = reduce<op, char   , char   >(in, dim); break;
        default:   TYPE_ERROR(1, type);
        }

        std::swap(*out, res);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #28
0
static af_err reduce_promote(af_array *out, const af_array in, const int dim)
{
    try {

        ARG_ASSERT(2, dim >= 0);
        ARG_ASSERT(2, dim <  4);

        const ArrayInfo in_info = getInfo(in);

        if (dim >= (int)in_info.ndims()) {
            *out = weakCopy(in);
            return AF_SUCCESS;
        }

        af_dtype type = in_info.getType();
        af_array res;

        switch(type) {
        case f32:  res = reduce<op, float  , float  >(in, dim); break;
        case f64:  res = reduce<op, double , double >(in, dim); break;
        case c32:  res = reduce<op, cfloat , cfloat >(in, dim); break;
        case c64:  res = reduce<op, cdouble, cdouble>(in, dim); break;
        case u32:  res = reduce<op, uint   , uint   >(in, dim); break;
        case s32:  res = reduce<op, int    , int    >(in, dim); break;
        case u8:   res = reduce<op, uchar  , uint   >(in, dim); break;
        case s8:   res = reduce<op, char   , int    >(in, dim); break;
            // Make sure you are adding only "1" for every non zero value, even if op == af_add_t
        case b8:   res = reduce<af_notzero_t, uchar  , uint   >(in, dim); break;
        default:   TYPE_ERROR(1, type);
        }
        std::swap(*out, res);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #29
0
af_err af_diff2(af_array *out, const af_array in, const int dim)
{

    try {

        ARG_ASSERT(2, ((dim >= 0) && (dim < 4)));

        ArrayInfo info = getInfo(in);
        af_dtype type = info.getType();

        af::dim4 in_dims = info.dims();
        DIM_ASSERT(1, in_dims[dim] >= 3);

        af_array output;

        switch(type) {
            case f32: output = diff2<float  >(in,dim);  break;
            case c32: output = diff2<cfloat >(in,dim);  break;
            case f64: output = diff2<double >(in,dim);  break;
            case c64: output = diff2<cdouble>(in,dim);  break;
            case b8:  output = diff2<char   >(in,dim);  break;
            case s32: output = diff2<int    >(in,dim);  break;
            case u32: output = diff2<uint   >(in,dim);  break;
            case s64: output = diff2<intl   >(in,dim);  break;
            case u64: output = diff2<uintl  >(in,dim);  break;
            case s16: output = diff2<short  >(in,dim);  break;
            case u16: output = diff2<ushort >(in,dim);  break;
            case u8:  output = diff2<uchar  >(in,dim);  break;
            default:  TYPE_ERROR(1, type);
        }
        std::swap(*out,output);
    }
    CATCHALL;

    return AF_SUCCESS;
}
예제 #30
0
af_err convert(af_array* out, const af_array in, const float r, const float g, const float b)
{
    try {
        ArrayInfo info     = getInfo(in);
        af_dtype iType     = info.getType();
        af::dim4 inputDims = info.dims();

        ARG_ASSERT(1, (inputDims.ndims()>=2));
        if (isRGB2GRAY) ARG_ASSERT(1, (inputDims[2]==3));

        af_array output = 0;
        switch(iType) {
            case f64: output = convert<double, double, isRGB2GRAY>(in, r, g, b); break;
            case f32: output = convert<float , float , isRGB2GRAY>(in, r, g, b); break;
            case u32: output = convert<uint  , float , isRGB2GRAY>(in, r, g, b); break;
            case s32: output = convert<int   , float , isRGB2GRAY>(in, r, g, b); break;
            case u8:  output = convert<uchar , float , isRGB2GRAY>(in, r, g, b); break;
            default: TYPE_ERROR(1, iType); break;
        }
        std::swap(*out, output);
    }
    CATCHALL;
    return AF_SUCCESS;
}