基于Cuda的开源张量计算库ArrayFire

28 篇文章 4 订阅
10 篇文章 0 订阅

基于Cuda的开源张量计算库ArrayFire

1、ArrayFire介绍

关于ArrayFire ArrayFire是一个高性能的软件库,可通过易于使用的API进行并行计算。 其基于数组的功能集使并行编程更易于访问。

    安装ArrayFire您可以使用Windows,OSX或Linux的二进制安装程序安装ArrayFire,也可以从源代码下载它:Windows,OSX和Linux的二进制安装程序从源代码构建易于使用数组对象非常简单。

    基于数组的符号有效地表达了类似于可读数学符号的计算算法。 您不需要并行编程方面的专门知识即可使用ArrayFire。

    几行ArrayFire代码完成了CUDA或OpenCL内核中可能需要数百条复杂代码的工作。

    ArrayFire可扩展

   支持多个域ArrayFire包含跨多个域的数百种功能,包括:矢量算法,图像处理,计算机视觉信号处理,线性代数统计等等。

ArrayFire开发人员使用所有可能的低级优化对每个功能进行了手动调整。

    支持各种数据类型和大小ArrayFire可处理常见的数据形状和大小,包括矢量,矩阵,体积,并且支持常见的数据类型,包括单精度和双精度浮点值,复数,布尔值以及32位有符号和 无符号整数。

    扩展ArrayFire ArrayFire可以用作独立应用程序,也可以与现有CUDA或OpenCL代码集成。 所有ArrayFire阵列都可以与其他CUDA或OpenCL数据结构互换。

    编写一次代码,即可在任何地方运行!

   凭借对x86,ARM,CUDA和OpenCL设备的支持,ArrayFire支持设备的完整列表。

    每个ArrayFire安装都附带:用于NVIDIA GPU的CUDA版本(命名为“ libafcuda”),用于OpenCL设备的OpenCL版本(命名为“ libafccl”)和CPU版本(命名为“ libafcpu”),当CUDA或OpenCL设备为 无法使用。

   ArrayFire是高效的矢量化和批处理操作ArrayFire支持N维数组上的批处理操作。  ArrayFire中的批处理操作并行运行,以确保CUDA或OpenCL设备的最佳使用。

    您可以使用矢量化技术从ArrayFire中获得最佳性能。

    ArrayFire还可以与gfor函数并行执行循环迭代。

    即时编译ArrayFire对您的代码执行运行时分析,以提高算术强度和内存吞吐量,同时避免不必要的临时分配。 它具有一个很棒的内部JIT编译器,可以为您进行优化。

阅读有关ArrayFire JIT如何提高应用程序性能的更多信息。

简单示例这是一个实时示例,可让您查看ArrayFire代码。 您创建驻留在CUDA或OpenCL设备上的阵列。 然后,您可以在这些阵列上使用ArrayFire函数。

#include <iostream>

#include <arrayfire.h>

using namespace af;

 

int main() {

         // sample 40 million points on the GPU

         array x = randu(20e6), y = randu(20e6);

         array dist = sqrt(x * x + y * y);

         // pi is ratio of how many fell in the unit circle

         float num_inside = sum<float>(dist < 1);

         float pi = 4.0 * num_inside / 20e7;

         af_print(pi);

         std::cout << pi << std::endl;

         return 0;

}

http://arrayfire.org/docs/installing.htm

http://arrayfire.org/docs/index.htm

2、ArrayFire提供的数据结构

ArrayFire 主要提供了8种数据结构,分别是array,dim4,event,exception, features,index,randomEngines,seq。

Data Structures

class  

array

 

A multi dimensional data container. 是一个多维数组容器,存储的数据可以是1维、二维、三维、四维,提供了多种构造函数。More...

class  

dim4

 

Generic object that represents size and shape. 表示有大小和形状的通用对象,More...

class  

event

 

C++ RAII interface for manipulating events. 表示可以操作的对象More...

class  

exception

 

An ArrayFire exception class. 处理ArrayFire异常的函数 More...

class  

features

 

Represents a feature returned by a feature detector. 表示特征探测器的返回结果More...

class  

index

 

Wrapper for af_index. 对索引的封装More...

class  

randomEngine

 

Random Number Generation Engine Class.随机数生成器 More...

class  

seq

 

seq is used to create sequences for indexing 带索引的序列数生成器af::array More

 

3、ArrayFire提供的函数

ArrayFire提供了200多种函数,用于处理各种计算任务,这些函数可以归为15类,分别是计算机视觉类,事件类,用于生成和修改矩阵的函数,用于矩阵内部处理的函数,图像处理,输入输出函数,接口函数,线性代数,机器学习,数学函数,内存管理,信号处理,统一接口函数,向量函数。

 

Computer Vision

 

A list of computer vision algorithms.一些计算机视觉的函数,包括特征描述,特征探测、特征匹配等,其中的函数大部分都要通过卷积运算完成。

Events

 

Managing ArrayFire Events which allows manipulation of operations on computation queues.处理与计算顺序有关的事件

Functions to create and modify Arrays

 

Array constructors, random number generation, transpose, indexing, etc.

矩阵的构造函数,随机数生成器,转置,索引,取行,取列,反转,数据在设备之间的移动,

 

Assignment & Indexing operation on arrays

 

Access sub regions of an array object.

 

Constructors of array class

 

Construct an array object.

 

Functions to create arrays.

 

constant, random, range, etc.

 

Helper functions for arrays

 

iszero, isInf, isNan, etc.

 

Managing devices in ArrayFire

 

getting device pointer, allocating and freeing memory

 

Methods of array class

 

Get information about the array object.

 

Move and Reorder array content

 

reorder, transpose, flip, join, tile, etc.

Functions to work with internal array layout

 

Functions to work with arrayfire's internal data structure.

arrayfire数组内部处理数据结构的函数

 

Image Processing

 

Image filtering, morphing and transformations.

图像处理相关函数,比如过滤,变形、变换等

 

Colorspace conversions颜色空间变换

 

RGB to gray, gray to RGB, RGB to HSV, etc.

 

 

Connected Components & Labeling计算连接区域或标签

 

regions

 

 

Filters各种过滤算法

 

bilateral, sobel, mean shift, median / min / max filters etc.

 

Histograms图像数据的直方图

 

Image and data histograms.

 

Image moments图像矩

 

Centroids, areas, etc.

 

 

Image transformations图像变换

 

rotate, skew, etc.

 

 

Morphological Operations图像变形操作

 

erode, dilate, etc.

 

 

Utility Functions工具函数,比如加载图片,保存图片,高斯核计算等

 

loadImage, saveImage, gaussianKernel

 

 

Wrapping and unwrapping image windows封装和未封装的图像窗口

 

wrap, unwrap, etc.

 

Input and Output functions输入输出函数

 

Functions to read and write data.用于读写数据图片等

 

Reading and writing arrays

 

printing data to screen / files

 

readArray

 

Load an array from a file.

 

 

saveArray

 

Save an array to a binary file.

 

 

Reading and writing images

 

Reading and writing images.

 

deleteImageMem

 

 

isImageIoAvailable

 

 

loadImage

 

 

loadImageMem

 

 

saveImage

 

 

saveImageMem

 

Interface Functions

 

Backend specific functions.

Modules

 

CUDA specific functions

 

Accessing ArrayFire's stream, and native device id with other CUDA code.

 

 

OpenCL specific functions

 

Accessing ArrayFire's context, queue, and share data with other OpenCL code.

 

Linear Algebra

 

Matrix multiply, solve, decompositions, sparse matrix.

Modules

 

BLAS operations

 

Matrix multiply, dot product, etc.

 

 

LAPACK Helper functions

 

 

Linear solve and least squares

 

solve, solveLU, etc.

 

 

Matrix factorizations and decompositions

 

LU, QR, Cholesky etc.

 

 

Matrix operations

 

inverse, det, rank, norm etc.

 

 

Sparse functions

 

Functions to create and handle sparse arrays and matrix operations.

 

Machine Learning

 

Machine learning functions.

Modules

 

Convolutions

 

Forward and backward convolution passes.

 

10 

Mathematical functions

 

Functions from standard math library.

Modules

 

Arithmetic operations

 

+, -, *, /, >>, <<

 

 

Complex operations

 

real, imag, conjugate etc.

 

 

Exponential and logarithmic functions

 

exp, log, expm1, log1p, etc.

 

 

Hyperbolic functions

 

sinh, cosh, tanh, etc.

 

 

Logical operations

 

&&, ||, |, &, <, >, <=, >=, ==, !

 

 

Numeric functions

 

floor, round, min, max, etc.

 

 

Trigonometric functions

 

sin, cos, tan, etc.

 

11 

Memory Management

 

Interfaces for writing custom memory managers.

Modules

 

Memory Manager API

 

Functions for defining custom memory managers.

 

 

Memory Manager Utils

 

Set and unset memory managers, set and get manager payloads, function setters.

 

 

Native Memory Interface

 

Native alloc, native free, get device id, etc.

 

12 

Signal Processing

 

Convolutions, FFTs, filters.

Modules

 

Convolutions

 

1D, 2D and 3D convolutions

 

 

Fast Fourier Transforms

 

1D, 2D and 3D forward, inverse FFTs

 

 

Filter

 

fir, iir, etc.

 

 

Interpolation and approximation

 

1D and 2D interpolation

 

13 

Statistics

 

A list of Statistics functions.

Modules

 

Basic statistics functions

 

mean, median, variance, etc.

 

14 

Unified API Functions

 

Functions to set current backend and utilities.

Modules

 

getActiveBackend

 

Get's the backend enum for the active backend.

 

 

getAvailableBackends

 

Returns an integer indicating the backends loaded successfully.

 

 

getBackendCount

 

Get the number of backends whose libraries were successfully loaded.

 

 

getBackendId

 

Get's the backend enum for an array.

 

 

getDeviceId

 

Get's the id of the device an array was created on.

 

 

setBackend

 

Set the current backend when using Unified backend.

 

15 

Vector Algorithms

 

sum, min, max, sort, set operations, etc.

Modules

 

Inclusive scan operations

 

inclusive / cumulative sum, etc.

 

 

Numerical differentiation

 

diff, gradient, etc.

 

 

Reduction operations

 

sum, min, max, etc.

 

 

Set operations

 

unique, union, intersect

 

 

Sort operations

 

sort, sort by key, etc.

 

4、ArryFire的可视化

这部分单独在另一篇中介绍。

5、代码示例(生命游戏细胞自动机)

这是一个细胞自动机模拟生命游戏的示例

#include <iostream>
#include <arrayfire.h>
using namespace af;

int main0()
{
	std::cout << "Hello World!\n";
	static const float h_kernel[] = { 1, 1, 1, 1, 0, 1, 1, 1, 1 };
	static const array kernel(3, 3, h_kernel, afHost);

	array state = (randu(512, 512, f32) > 0.5).as(f32); // Generate starting state
	Window myWindow(512, 512);
	while (!myWindow.close()) {
		array nHood = convolve(state, kernel); // Obtain neighbors
		array C0 = (nHood == 2);               // Generate conditions for life
		array C1 = (nHood == 3);
		state = state * C0 + C1;               // Update state
		myWindow.image(state);                 // Display
	}
	return EXIT_SUCCESS;
}

结果如下,可以看出计算效率非常高,显示很顺滑,即使矩阵增大为1024*1024,由于cuda的加持,实时显示仍很顺滑。

 

http://arrayfire.org/docs/group__arrayfire__class.htm

http://arrayfire.org/docs/group__arrayfire__func.htm

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oceanstonetree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值