helloworld/helloworld.cpp
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* https://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <cstdio>
#include <cstdlib>
using namespace af;
int main(int argc, char* argv[]) {
try {
// Select a device and display arrayfire info
int device = argc > 1 ? atoi(argv[1]) : 0;
af::setDevice(device);
printf("Create a 5-by-3 matrix of random floats on the GPU\n");
array A = randu(5, 3, f32);
printf("Element-wise arithmetic\n");
array B = sin(A) + 1.5;
printf("Negate the first three elements of second column\n");
B(seq(0, 2), 1) = B(seq(0, 2), 1) * -1;
printf("Fourier transform the result\n");
array C = fft(B);
printf("Grab last row\n");
array c = C.row(end);
printf("Scan Test\n");
dim4 dims(16, 4, 1, 1);
array r = constant(2, dims);
printf("Scan\n");
printf("Create 2-by-3 matrix from host data\n");
float d[] = {1, 2, 3, 4, 5, 6};
array D(2, 3, d, afHost);
printf("Copy last column onto first\n");
D.col(0) = D.col(end);
// Sort A
printf("Sort A and print sorted array and corresponding indices\n");
array vals, inds;
sort(vals, inds, A);
af_print(vals);
af_print(inds);
} catch (af::exception& e) {
fprintf(stderr, "%s\n", e.what());
throw;
}
return 0;
}
af::dim4
Generic object that represents size and shape.
Definition: dim4.hpp:33
af::seq
seq is used to create sequences for indexing af::array
Definition: seq.h:46
af::info
AFAPI void info()
af::constant
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
af::setDevice
AFAPI void setDevice(const int device)
Sets the current device.
af::sort
AFAPI array sort(const array &in, const unsigned dim=0, const bool isAscending=true)
C++ Interface for sorting an array.
af::array::col
array::array_proxy col(int index)
Returns a reference to a col.
af::array
A multi dimensional data container.
Definition: array.h:35
afHost
@ afHost
Host pointer.
Definition: defines.h:234
af::scan
AFAPI array scan(const array &in, const int dim=0, binaryOp op=AF_BINARY_ADD, bool inclusive_scan=true)
C++ Interface generalized scan of an array.
af
Definition: algorithm.h:15
af_print
#define af_print(...)
Definition: util.h:148
AF_BINARY_MUL
@ AF_BINARY_MUL
Definition: defines.h:431
af::sin
AFAPI array sin(const array &in)
C++ Interface for sin.
af::randu
AFAPI array randu(const dim4 &dims, const dtype ty, randomEngine &r)
af::array::row
array::array_proxy row(int index)
Returns a reference to a row.
af::exception
An ArrayFire exception class.
Definition: exception.h:29
af::fft
AFAPI array fft(const array &in, const dim_t odim0=0)
C++ Interface for fast fourier transform on one dimensional signals.
arrayfire.h
af::exception::what
virtual const char * what() const
Returns an error message for the exception in a string format.
Definition: exception.h:60
af::end
AFAPI int end
A special value representing the last value of an axis.
f32
@ f32
32-bit floating point values
Definition: defines.h:211