Code Monkey home page Code Monkey logo

inversefx's Introduction

InverseFX

Computing a function when only its inverse is known, using Newson-Raphson method for 1D,2D,3D arrays in parallel. When user has parallelized version of f(x), it becomes faster. The overall speedup is about 7x for AVX and 15x for AVX512 with x-square function.

Requirements:

  • C++14 compiler
  • Auto-vectorization of compiler
  • Auto-vectorization flags -std=c++14 -O3 -march=native -mavx2 -mprefer-vector-width=256 -ftree-vectorize -fno-math-errno
  • Or AVX512 version of flags and 512 vector width

How it works:

  • user has an f(x) function given to the constructor
  • constructor also takes h(step length) argument for the discrete-derivative calculations such as "two point derivative"/"five point stencil"
  • whenever inverse of f(x) at x=x0 is needed, user can call obj.computeInverse...() method
  • the computeInverse..() method uses Newton-Raphson method and discrete-derivative to approach inverse f(x0)

Usage:

  • parallelized (fast) inversion
InverseFX::ParallelInverse<float> invPar(
        // user's parallel f(x) function 
        // maps one to one from inp to out, for n elements from first element
        [](float * inp, float * out, int n){
		// C++ compiler vectorizes simple loop easily and possibly inlines this lambda for efficient SIMD
		for(int i=0;i<n;i++)
		{
		    out[i]=inp[i]*inp[i]; // square of x
		}
	},
        // h value that is used for computing two-point derivative inside the inversion logic
        0.001f
);

float inp[n],outp[n];

// compute inverse of f(x) at all points (n elements), reading from inp and writing result to outp
invPar.computeInverseLowQuality(inp,outp,n); // square-root is found for n-elements
  • parallelized inversion using scalar f(x) function
InverseFX::ParallelInverse<float> invPar(
      // f(x)=x*x, for answering question of "what is inverse of x*x?"
      // not as efficient as the parallel f(x) but rest of the algorithm is still parallelized
      [](float inp){ return inp*inp;},
      0.001f /* h */
);

float inp[n],outp[n];

// compute inverse of f(x) at all points (n elements), reading from inp and writing result to outp
invPar.computeInverseLowQuality(inp,outp,n); 
  • scalar inversion on scalar f(x) function
InverseFX::ScalarInverse<float> inv([](float inp){
	// black-box function sample
	// f(x)=x*x
	return inp*inp;
},0.001f);

// inverse of x*x is sqrt(x)
float squareRootOfPI = inv.computeInverseLowQuality(3.1415f);

inversefx's People

Contributors

tugrul512bit avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

mfkiwl

inversefx's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.