Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions include/af/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,19 @@ extern "C" {
*/
AFAPI af_err af_sum(af_array *out, const af_array in, const int dim);

#if AF_API_VERSION >= 39
/**
C Interface for sum of all elements in an array, resulting in an array

\param[out] out will contain the sum of all values in \p in
\param[in] in is the input array
\return \ref AF_SUCCESS if the execution completes properly

\ingroup reduce_func_sum
*/
AFAPI af_err af_sum_all_array(af_array *out, const af_array in);
#endif

#if AF_API_VERSION >= 31
/**
C Interface for sum of elements in an array while replacing nans
Expand All @@ -690,6 +703,21 @@ extern "C" {
const int dim, const double nanval);
#endif

#if AF_API_VERSION >= 39
/**
C Interface for sum of all elements in an array, resulting in an array with
nan substitution

\param[out] out will contain the sum of all values in \p in
\param[in] in is the input array
\param[in] nanval The value that will replace the NaNs in \p in
\return \ref AF_SUCCESS if the execution completes properly

\ingroup reduce_func_sum
*/
AFAPI af_err af_sum_nan_all_array(af_array *out, const af_array in, const double nanval);
#endif

#if AF_API_VERSION >= 37
/**
C Interface for sum of elements in an array according to key
Expand Down Expand Up @@ -741,6 +769,19 @@ extern "C" {
*/
AFAPI af_err af_product(af_array *out, const af_array in, const int dim);

#if AF_API_VERSION >= 39
/**
C Interface for product of elements in an array, resulting in an array

\param[out] out will contain the product of all values in \p in
\param[in] in is the input array
\return \ref AF_SUCCESS if the execution completes properly

\ingroup reduce_func_product
*/
AFAPI af_err af_product_all_array(af_array *out, const af_array in);
#endif

#if AF_API_VERSION >= 31
/**
C Interface for product of elements in an array while replacing nans
Expand All @@ -757,6 +798,21 @@ extern "C" {
AFAPI af_err af_product_nan(af_array *out, const af_array in, const int dim, const double nanval);
#endif

#if AF_API_VERSION >= 39
/**
C Interface for product of elements in an array, resulting in an array
while replacing nans

\param[out] out will contain the product of all values in \p in
\param[in] in is the input array
\param[in] nanval The value that will replace the NaNs in \p in
\return \ref AF_SUCCESS if the execution completes properly

\ingroup reduce_func_product
*/
AFAPI af_err af_product_nan_all_array(af_array *out, const af_array in, const double nanval);
#endif

#if AF_API_VERSION >= 37
/**
C Interface for product of elements in an array according to key
Expand Down Expand Up @@ -1052,6 +1108,19 @@ extern "C" {
*/
AFAPI af_err af_min_all(double *real, double *imag, const af_array in);

#if AF_API_VERSION >= 39
/**
C Interface for minimum values in an array, returning an array

\param[out] out will contain the minimum of all values in \p in
\param[in] in is the input array
\return \ref AF_SUCCESS if the execution completes properly

\ingroup reduce_func_min
*/
AFAPI af_err af_min_all_array(af_array *out, const af_array in);
#endif

/**
C Interface for getting maximum value of an array

Expand All @@ -1066,6 +1135,21 @@ extern "C" {
*/
AFAPI af_err af_max_all(double *real, double *imag, const af_array in);

#if AF_API_VERSION >= 39
/**
C Interface for getting maximum value of an array, returning an array

\param[out] out will contain the maximum of all values in \p in
\param[in] in is the input array
\return \ref AF_SUCCESS if the execution completes properly

\note \p imag is always set to 0 when \p in is real.

\ingroup reduce_func_max
*/
AFAPI af_err af_max_all_array(af_array *out, const af_array in);
#endif

/**
C Interface for checking if all values in an array are true

Expand All @@ -1080,6 +1164,22 @@ extern "C" {
*/
AFAPI af_err af_all_true_all(double *real, double *imag, const af_array in);

#if AF_API_VERSION >= 39
/**
C Interface for checking if all values in an array are true,
while returning an af_array

\param[out] out will contain 1 if all values of input \p in are true, 0 otherwise
\param[in] in is the input array
\return \ref AF_SUCCESS if the execution completes properly

\note \p imag is always set to 0.

\ingroup reduce_func_all_true
*/
AFAPI af_err af_all_true_all_array(af_array *out, const af_array in);
#endif

/**
C Interface for checking if any values in an array are true

Expand All @@ -1094,6 +1194,22 @@ extern "C" {
*/
AFAPI af_err af_any_true_all(double *real, double *imag, const af_array in);

#if AF_API_VERSION >= 39
/**
C Interface for checking if any values in an array are true,
while returning an af_array

\param[out] out will contain 1 if any value of input \p in is true, 0 otherwise
\param[in] in is the input array
\return \ref AF_SUCCESS if the execution completes properly

\note \p imag is always set to 0.

\ingroup reduce_func_any_true
*/
AFAPI af_err af_any_true_all_array(af_array *out, const af_array in);
#endif

/**
C Interface for counting total number of non-zero values in an array

Expand All @@ -1108,6 +1224,20 @@ extern "C" {
*/
AFAPI af_err af_count_all(double *real, double *imag, const af_array in);

#if AF_API_VERSION >= 39
/**
C Interface for counting total number of non-zero values in an array,
while returning an af_array

\param[out] out contain the number of non-zero values in \p in.
\param[in] in is the input array
\return \ref AF_SUCCESS if the execution completes properly

\ingroup reduce_func_count
*/
AFAPI af_err af_count_all_array(af_array *out, const af_array in);
#endif

/**
C Interface for getting minimum values and their locations in an array

Expand Down
4 changes: 3 additions & 1 deletion src/api/c/anisotropic_diffusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using common::cast;
using detail::arithOp;
using detail::Array;
using detail::createEmptyArray;
using detail::getScalar;
using detail::gradient;
using detail::reduce_all;

Expand All @@ -48,7 +49,8 @@ af_array diffusion(const Array<float>& in, const float dt, const float K,
auto g0Sqr = arithOp<float, af_mul_t>(g0, g0, dims);
auto g1Sqr = arithOp<float, af_mul_t>(g1, g1, dims);
auto sumd = arithOp<float, af_add_t>(g0Sqr, g1Sqr, dims);
float avg = reduce_all<af_add_t, float, float>(sumd, true, 0);
float avg =
getScalar<float>(reduce_all<af_add_t, float, float>(sumd, true, 0));

anisotropicDiffusion(out, dt, 1.0f / (cnst * avg), fftype, eq);
}
Expand Down
14 changes: 9 additions & 5 deletions src/api/c/canny.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ using detail::createEmptyArray;
using detail::createHostDataArray;
using detail::createSubArray;
using detail::createValueArray;
using detail::getScalar;
using detail::histogram;
using detail::iota;
using detail::ireduce;
Expand Down Expand Up @@ -151,7 +152,9 @@ pair<Array<char>, Array<char>> computeCandidates(const Array<float>& supEdges,
const float t1,
const af_canny_threshold ct,
const float t2) {
float maxVal = reduce_all<af_max_t, float, float>(supEdges);
float maxVal =
getScalar<float>(reduce_all<af_max_t, float, float>(supEdges));
;
auto NUM_BINS = static_cast<unsigned>(maxVal);

auto lowRatio = createValueArray<float>(supEdges.dims(), t1);
Expand All @@ -171,10 +174,11 @@ pair<Array<char>, Array<char>> computeCandidates(const Array<float>& supEdges,
return make_pair(strong, weak);
};
default: {
float minVal = reduce_all<af_min_t, float, float>(supEdges);
auto normG = normalize(supEdges, minVal, maxVal);
auto T2 = createValueArray<float>(supEdges.dims(), t2);
auto T1 = createValueArray<float>(supEdges.dims(), t1);
float minVal =
getScalar<float>(reduce_all<af_min_t, float, float>(supEdges));
auto normG = normalize(supEdges, minVal, maxVal);
auto T2 = createValueArray<float>(supEdges.dims(), t2);
auto T1 = createValueArray<float>(supEdges.dims(), t1);
Array<char> weak1 =
logicOp<float, af_ge_t>(normG, T1, normG.dims());
Array<char> weak2 =
Expand Down
19 changes: 12 additions & 7 deletions src/api/c/confidence_connected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ using common::createSpanIndex;
using detail::arithOp;
using detail::Array;
using detail::createValueArray;
using detail::getScalar;
using detail::reduce_all;
using detail::uchar;
using detail::uint;
Expand Down Expand Up @@ -127,8 +128,8 @@ af_array ccHelper(const Array<T>& img, const Array<uint>& seedx,
Array<CT> I2 = common::integralImage<CT>(in_2);
Array<CT> S1 = sum(I1, _x, x_, _y, y_);
Array<CT> S2 = sum(I2, _x, x_, _y, y_);
CT totSum = reduce_all<af_add_t, CT, CT>(S1);
CT totSumSq = reduce_all<af_add_t, CT, CT>(S2);
CT totSum = getScalar<CT>(reduce_all<af_add_t, CT, CT>(S1));
CT totSumSq = getScalar<CT>(reduce_all<af_add_t, CT, CT>(S2));
CT totalNum = numSeeds * nhoodSize;
CT s1mean = totSum / totalNum;
CT s1var = calcVar(totSumSq, totSum, totalNum);
Expand All @@ -137,8 +138,10 @@ af_array ccHelper(const Array<T>& img, const Array<uint>& seedx,
CT upper = s1mean + mult * s1stddev;

Array<CT> seedIntensities = pointList(in, seedx, seedy);
CT maxSeedIntensity = reduce_all<af_max_t, CT, CT>(seedIntensities);
CT minSeedIntensity = reduce_all<af_min_t, CT, CT>(seedIntensities);
CT maxSeedIntensity =
getScalar<CT>(reduce_all<af_max_t, CT, CT>(seedIntensities));
CT minSeedIntensity =
getScalar<CT>(reduce_all<af_min_t, CT, CT>(seedIntensities));

if (lower > minSeedIntensity) { lower = minSeedIntensity; }
if (upper < maxSeedIntensity) { upper = maxSeedIntensity; }
Expand All @@ -155,16 +158,18 @@ af_array ccHelper(const Array<T>& img, const Array<uint>& seedx,
// Segmented images are set with 1's and 0's thus essentially
// making them into mask arrays for each iteration's input image

uint sampleCount = reduce_all<af_notzero_t, CT, uint>(segmented, true);
uint sampleCount = getScalar<uint>(
reduce_all<af_notzero_t, CT, uint>(segmented, true));
if (sampleCount == 0) {
// If no valid pixels are found, skip iterations
break;
}
Array<CT> valids = arithOp<CT, af_mul_t>(segmented, in, inDims);
Array<CT> vsqrd = arithOp<CT, af_mul_t>(valids, valids, inDims);

CT validsSum = reduce_all<af_add_t, CT, CT>(valids, true);
CT sumOfSqs = reduce_all<af_add_t, CT, CT>(vsqrd, true);
CT validsSum =
getScalar<CT>(reduce_all<af_add_t, CT, CT>(valids, true));
CT sumOfSqs = getScalar<CT>(reduce_all<af_add_t, CT, CT>(vsqrd, true));
CT validsMean = validsSum / sampleCount;
CT validsVar = calcVar(sumOfSqs, validsSum, CT(sampleCount));
CT stddev = sqrt(validsVar);
Expand Down
12 changes: 7 additions & 5 deletions src/api/c/corrcoef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <backend.hpp>
#include <common/cast.hpp>
#include <common/err_common.hpp>
#include <copy.hpp>
#include <handle.hpp>
#include <math.hpp>
#include <reduce.hpp>
Expand All @@ -26,6 +27,7 @@ using af::dim4;
using common::cast;
using detail::arithOp;
using detail::Array;
using detail::getScalar;
using detail::intl;
using detail::reduce_all;
using detail::uchar;
Expand All @@ -41,16 +43,16 @@ static To corrcoef(const af_array& X, const af_array& Y) {
const dim4& dims = xIn.dims();
dim_t n = xIn.elements();

To xSum = reduce_all<af_add_t, To, To>(xIn);
To ySum = reduce_all<af_add_t, To, To>(yIn);
To xSum = getScalar<To>(reduce_all<af_add_t, To, To>(xIn));
To ySum = getScalar<To>(reduce_all<af_add_t, To, To>(yIn));

Array<To> xSq = arithOp<To, af_mul_t>(xIn, xIn, dims);
Array<To> ySq = arithOp<To, af_mul_t>(yIn, yIn, dims);
Array<To> xy = arithOp<To, af_mul_t>(xIn, yIn, dims);

To xSqSum = reduce_all<af_add_t, To, To>(xSq);
To ySqSum = reduce_all<af_add_t, To, To>(ySq);
To xySum = reduce_all<af_add_t, To, To>(xy);
To xSqSum = getScalar<To>(reduce_all<af_add_t, To, To>(xSq));
To ySqSum = getScalar<To>(reduce_all<af_add_t, To, To>(ySq));
To xySum = getScalar<To>(reduce_all<af_add_t, To, To>(xy));

To result =
(n * xySum - xSum * ySum) / (std::sqrt(n * xSqSum - xSum * xSum) *
Expand Down
4 changes: 3 additions & 1 deletion src/api/c/gaussian_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <arith.hpp>
#include <backend.hpp>
#include <common/err_common.hpp>
#include <copy.hpp>
#include <handle.hpp>
#include <math.hpp>
#include <range.hpp>
Expand All @@ -24,6 +25,7 @@ using af::dim4;
using detail::arithOp;
using detail::Array;
using detail::createValueArray;
using detail::getScalar;
using detail::range;
using detail::reduce_all;
using detail::scalar;
Expand Down Expand Up @@ -77,7 +79,7 @@ Array<T> gaussianKernel(const int rows, const int cols, const double sigma_r,

// Use this instead of (2 * pi * sig^2);
// This ensures the window adds up to 1
T norm_factor = reduce_all<af_add_t, T, T>(tmp);
T norm_factor = getScalar<T>(reduce_all<af_add_t, T, T>(tmp));

Array<T> norm = createValueArray(odims, norm_factor);
Array<T> res = arithOp<T, af_div_t>(tmp, norm, odims);
Expand Down
5 changes: 4 additions & 1 deletion src/api/c/hist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <common/cast.hpp>
#include <common/err_common.hpp>
#include <common/graphics_common.hpp>
#include <copy.hpp>
#include <handle.hpp>
#include <hist_graphics.hpp>
#include <reduce.hpp>
Expand All @@ -20,6 +21,7 @@
using detail::Array;
using detail::copy_histogram;
using detail::forgeManager;
using detail::getScalar;
using detail::uchar;
using detail::uint;
using detail::ushort;
Expand Down Expand Up @@ -57,7 +59,8 @@ fg_chart setup_histogram(fg_window const window, const af_array in,
float xMin, xMax, yMin, yMax, zMin, zMax;
FG_CHECK(_.fg_get_chart_axes_limits(&xMin, &xMax, &yMin, &yMax, &zMin,
&zMax, chart));
T freqMax = detail::reduce_all<af_max_t, T, T>(histogramInput);
T freqMax =
getScalar<T>(detail::reduce_all<af_max_t, T, T>(histogramInput));

if (xMin == 0 && xMax == 0 && yMin == 0 && yMax == 0) {
// No previous limits. Set without checking
Expand Down
Loading