Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to Add a New Function to ArrayFire

pradeep edited this pageJul 15, 2021 ·10 revisions

Listed below are the necessary files inside the directory structure that youneed to add or modify for implementing a new function in ArrayFire. We havean example function (exampleFunction) built into the library itself, whichshows how a new developer might go about implementing a new function inArrayFire. It demonstrates how the user-facing C/C++ API talks to each backend.You can use its existing files as a guide or as a starting point for yourimplementation (of course by making a copy of the files and then modifying them).Note, however, that you may need to add or modify other files, depending on thesituation.

A quick layout is given below to serve as a quick reference on what files mustbe added and/or modified, and where they are located. Further below, a moredetailed layout is given, which describes the purpose of each file and someimportant details to take into account when adding/modifying those files.

After you have added and modified all the necessary files for your function, youare welcome to submit a pull request to ArrayFire. Doing so will queue yourchanges for building and testing in our continuous integration pipeline (makesure that you have created tests indeed!), but at least one of us needs to reviewand approve your changes before merging them in.

Quick layout

include |__ af/<domain>.h |__ arrayfire.hsrc |__ api |    |__ c |    |    |__ exampleFunction.cpp |    |    |__ CMakeLists.txt |    | |    |__ cpp |    |    |__ <domain>.cpp |    |    |__ CMakeLists.txt |    | |    |__ unified |         |__ <domain>.cpp |         |__ CMakeLists.txt | |__ backend      |__ cpu      |    |__ exampleFunction.[hpp|cpp]      |    |__ kernel      |    |    |__ exampleFunction.hpp      |    |__ CMakeLists.txt      |      |__ cuda      |    |__ exampleFunction.[hpp|cu]      |    |__ kernel      |    |    |__ exampleFunction.hpp      |    |__ CMakeLists.txt      |      |__ opencl           |__ exampleFunction.[hpp|cpp]           |__ kernel           |    |__ exampleFunction.hpp           |    |__ exampleFunction.cl           |__ CMakeLists.txttest |__ exampleFunction.cppdocs |__ details/<domain>.doxCMakeModules - CMake Find scripts for ArrayFire dependenciesexamples - Code examples using ArrayFire

Detailed Layout

Include

  • include/af/<domain>.h

    • Contains the C and C++ declarations of your new function
    • If you are working on a function that already belongs to an existing groupsuch as image processing, statistics, etc., add your declarations to thatgroup/domain. Otherwise, create a header and include it ininclude/arrayfire.h
    • Add both the C and C++ declarations for your new function here.
    • Prefix the C function name withaf_.
    • If the C++ function has any arguments with default values, initializethose arguments here
    • Add the C and C++ function's documentation here (Doxygen style)
  • include/arrayfire.h

    • Contains the includes for the domain header files
    • Include your new af/<domain>.h header file here if you do create anew one

C API

  • src/api/c/exampleFunction.cpp

    • Contains the definition of the function's C API.
    • If arrayfire already has all the functions needed for this new function,try first to keep all your implementation code here rather than addingcode tosrc/backend
  • src/api/c/CMakeLists.txt

    • Add yourinclude/af/<domain>.h header andsrc/api/c/exampleFunction.cpp in their appropriate sections here

C++ API

  • src/api/cpp/<domain>.cpp

    • Contains the definition of the function's C++ API. Most of the time, all youessentially have to do here is call the C API function.
    • See the other C++ functions in the same<domain>.cpp filefor examples
  • src/api/cpp/CMakeLists.txt (should already exist)

    • Add your<domain>.cpp file here

Unified Backend

  • src/api/unified/<domain>.cpp

    • Contains API delegation from unified level to C API level
    • See the other C++ functions in the same<domain>.cpp filefor examples
  • src/api/unified/CMakeLists.txt (should already exist)

    • Add your<domain>.cpp file here

CPU Backend

  • src/backend/cpu/exampleFunction.[hpp|cpp]

    • Wrapper level for the CPU algorithm, which can call yourown and/or other existing CPU kernels
    • You can write the whole implementation here if all the necessarykernels already exist
  • src/backend/cpu/kernel/exampleFunction.hpp

    • Contains the CPU-specific implementation of the algorithm here
  • src/backend/cpu/CMakeLists.txt (should already exist)

    • Add yourexampleFunction.[hpp|cpp] andkernel/exampleFunction.hpp to their appropriate sectionshere

CUDA Backend

  • src/backend/cuda/exampleFunction.[hpp|cu]

    • Wrapper level for the CUDA algorithm, which can call yourown and/or other existing CUDA kernels
    • You can write the whole implementation here if all the necessarykernels already exist
  • src/backend/cuda/kernel/exampleFunction.hpp

    • Contains the CUDA-specific implementation of the algorithm here
  • src/backend/cuda/CMakeLists.txt (should already exist)

    • Add your exampleFunction.[hpp|cu] andkernel/exampleFunction.hpp to their appropriate sectionshere

OpenCL Backend

  • src/backend/opencl/exampleFunction.[hpp|cpp]

    • C++ wrapper level for the OpenCL kernel wrappers, which can call yourown and/or other existing OpenCL kernel wrappers
    • You can write the whole implementation here if all the necessaryOpenCL kernel wrappers already exist
  • src/backend/opencl/kernel/exampleFunction.hpp

    • Contains the OpenCL kernel wrappers
    • Note that because of the nature of how OpenCL operates, this file does notactually link with theexampleFunction.cl file. Rather, this includeskernel_headers/exampleFunction.hpp, which contains the OpenCL kernel in theform of a runtime char array. This char array is passed to OpenCL API callsto dispatch the actual OpenCL kernel.
  • src/backend/opencl/kernel/exampleFunction.cl (OpenCL Kernel file)

    • Contains the actual OpenCL kernels
    • See the note above about how this file is "linked" with exampleFunction.hpp.To support the described scheme above, a header file (inkernel_headers/)is generated during the build process, which basically creates a char arrayobject representation of this file.
  • src/backend/opencl/CMakeLists.txt (should already exist)

    • Add yourexampleFunction.[hpp|cpp] andkernel/exampleFunction.hpp totheir appropriate sections here

Tests

  • test/exampleFunction.cpp

    • Contains unit tests for the new function. Refer to theWriting unit testssection of the wiki for this part.
    • It is good practice to add a test for the documentation's code snippets here,to verify that the public-facing example indeed works
  • test/build/extern/af_test_data-src/exampleFunction/<test_data_name>.test

    • Test data files are fetched during CMake configure stage fromarrayfire-data repository. Any changes to test data are to be raised as pull requests to thearrayfire-data repository. If such changes are done, then corresponding git hash needs to be updated in ArrayFire'stest/CMakeLists.txt file.
    • Refer to theUnit test data format page to understand the format in which test data needs to be added toarrayfire-data repository. You can have multiple test files if needed.

Documentation

ArrayFire is documented usingDoxygen. The goal with the documentation is to provide function information in the headers and via HTML pages.

  • docs/details/<domain>.dox
    • Contains the Doxygen markup for the domain's functions
    • Add your functions' brief and detailed descriptions here. Note that you canadd references to code snippets here for demonstration purposes, which areactually displayed in the description when rendered. Ideally, they would bepart of the test suite for the same function(s)
    • See existing documentation sections for examples
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp