Skip to content
C computation graph, AutoGrad with OpenCL support [WIP]
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



C Computation Graph Library

Build Status:

Build Status

  • OpenCL API is still WIP, still learning it .. Turn off CG_USE_OPENCL option(CG_USE_OPENCL "OpenCL acceleration" OFF) in the root CMakeLists.txt file.

  • Development being done on Mac OS, with few tests on Ubuntu 18 LTS.


CGraph, short for C Computation Graph is a C library for building Tensor graphs and automatic differenciation.


Uses BLAS for complex operations.

The current version focuses on clear code rather than highly performant one. Once everything is well tested, hard optimizations such as switch statements and others will be improved. Also not all operations are written in blas, some uses classic for-loop, as I am still learning BLAS, optimizations will come once the library becomes stable.


  • LAPACK: sudo apt-get install libblas-dev liblapack-dev
  • cmake sudo apt-get install cmake
  • probably build-essentials as well.
  • lua5.1 at least. LuaJIT is not support due to its 1~2Gb memory limitation.
  • cairo for kplot

OpenCL dependencies

  • Currently tested only on ubuntu 16.04 LTS and Ubuntu 18.04.1 LTS (Plasma Desktop, I know it has nothing to do, just wanted to say it)
  • Matrices are by default Row major
  • Vectors are treated as column matrices


  • Double numbers only.

C API Status:

  • Memory management has been greatly improved, but needs more checking, especilly with the gradient calculation
  • C API is almost stable.
  • Could create more sanity check APIs
  • Could use more unittests.

Lua API status:

  • Working but need memory improvements
  • No unittest
  • Easy to setup and use, but not yet reliable.

Example (Lua API):

local CGraph = require 'CGraph'
local array = CGraph.array

local function sigmoid(z)
	local Z = CGraph.variable("z")
	local sigmoid = CGraph.double(1) / (CGraph.double(1) + CGraph.exp(-Z))
	local graph = CGraph.graph("sigmoid", sigmoid)
	graph:setVar("z", z)
	local res = graph:eval()
	return res

print(sigmoid( CGraph.vector(3, array {0,0,0}), CGraph.vector(3, array {0,0,0}) )))

return sigmoid

graph:plot() will plot a graph as a dot which can be transformed into a png with graphviz's dot command: dot -Tpng -o sigmoid.png.


Building and Running Neural Network Example

  • You would need lua socket installed sudo apt-get install lua-socket lua-sec
  • Or manually download iris dataset and place it into datasets/Iris.csv.

Compiling C and Lua API

cd source
mkdir build
cd build
cmake ..
cd ../../lua_api
mv source/build/lua_api/ ./

Then, from examples_lua directory

lua iris.lua


If you had an error while running a Lua script, you can debug it as follows:

gdb lua
(gdb) source luagdb.txt
(gdb) run iris.lua

And from there you have access to the C API from gdb.

Future work

  • Graph variables (Done)
  • Lua API for graph construction (Done)
  • Derivative calculations (Done)
  • Usage of BLAS in all operations (In progress)
  • GPU BLAS implementation (clBLAS probably & raw OpenCL as well)
  • Multithreaded implementation
  • Graph plotting and visualization (Done, outdated)
  • Switch to LuaJIT instead of Lua API (Must do ASAP)
  • Travis CI (done)
  • Valgrind to check memory (Done)
  • Optimal data fetching and allocation (Lazy evaluation)

Dependencies included within the source code:


If you would like to contribute, feel free to fork this stuff. A wonderful start would be to include a unit test file to check all the functionalities of either API.

Here is a list of the things I want to add:

  • Better plotting tools
  • Image filters
  • io subject, csv, json, xml, tar, etc
  • Dataset loaders i.e mnist
  • Propose anything you want.
What I really insist on:
  • Pure C API (might even remove CLBlast if I can get to write my own kernels)
  • No global variables & Thread Safety
  • Memory profiling
  • Seperation of intel libraries and public API (CAPI project)
You can’t perform that action at this time.