Do more. Code less. Free software for GPU computing.
<scroll to top>

examples/misc/any.cpp

#include <stdio.h>
#include <cuda.h>
#include <cuda_runtime_api.h>
#include <assert.h>
#include <arrayfire.h>

using namespace af;

void any_example_vector()
{
    array bool1 = zeros(16) > 0.5;
    array bool2 = ones(16) > 0.5;
    array bool3 = randu(16) > 0.5;

    bool result1, result2, result3;

    if (af_anytrue_vector(&result1, bool1.elements(), bool1.device<bool>()) != AF_SUCCESS ||
        af_anytrue_vector(&result2, bool2.elements(), bool2.device<bool>()) != AF_SUCCESS ||
        af_anytrue_vector(&result3, bool3.elements(), bool3.device<bool>()) != AF_SUCCESS) {
        fprintf(stderr, "Failure while calling ANY!\n");
        return;
    }

    assert(result1 == false && result2 == true);
    printf("There were %sones in the third set.\n", result3 ? "" : "NO ");
}

void any_example_rows()
{
    array bool1 = randu(4, 4) > 0.5;
    bool results[4] = {0};
    bool *r = array::alloc<bool>(4);

    unsigned dims[] = {4,4};
    if (af_anytrue_B(r, 2, dims, bool1.device<bool>(), 1) != AF_SUCCESS) {
        fprintf(stderr, "Failure while calling ANY!\n");
        return;
    }

    cudaMemcpy(results, r, sizeof(bool)*4, cudaMemcpyDeviceToHost);
    array::free(r);

    for (int n = 0; n < 4; n++)
        printf("There were %sones in the third set, row %d.\n", results[n] ? "" : "no ", n);
}

int main(int argc, char ** argv)
{
    try {
        // Perform the example
        any_example_vector();
        any_example_rows();
    } catch (af::exception& e) {
        fprintf(stderr, "%s\n", e.what());
    }

    #ifdef WIN32 // pause in Windows
    if (!(argc == 2 && argv[1][0] =='-')) {
        printf("hit [enter]...");
        getchar();
    }
    #endif
    return 0;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines