| Andrew Cooke | Contents | Latest | RSS | Twitter | Previous | Next

C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Lepl parser for Python.

Colorless Green.

Photography around Santiago.

SVG experiment.

Professional Portfolio

Calibration of seismometers.

Data access via web services.

Cache rewrite.

Extending OpenSSH.

Last 100 entries

Poorest in UK, Poorest in N Europe; I Want To Be A Redneck!; Reverse Racism; The Lost Art Of Nomography; IBM Data Center (Photo); Interesting Account Of Gamma Hack; The Most Interesting Audiophile In The World; How did the first world war actually end?; Ky - Restaurant Santiago; The Black Dork Lives!; The UN Requires Unaninmous Decisions; LPIR - Steganography in Practice; How I Am 6; Clear Explanation of Verizon / Level 3 / Netflix; Teenage Girls; Formalising NSA Attacks; Switching Brakes (Tektro Hydraulic); Naim NAP 100 (Power Amp); AKG 550 First Impressions; Facebook manipulates emotions (no really); Map Reduce "No Longer Used" At Google; Removing RAID metadata; New Bike (Good Bike Shop, Santiago Chile); Removing APE Tags in Linux; Compiling Python 3.0 With GCC 4.8; Maven is Amazing; Generating Docs from a GitHub Wiki; Modular Shelves; Bash Best Practices; Good Emergency Gasfiter (Santiago, Chile); Readings in Recent Architecture; Roger Casement; Integrated Information Theory (Or Not); Possibly undefined macro AC_ENABLE_SHARED; Update on Charges; Sunburst Visualisation; Spectral Embeddings (Distances -> Coordinates); Introduction to Causality; Filtering To Help Colour-Blindness; ASUS 1015E-DS02 Too; Ready Player One; Writing Clear, Fast Julia Code; List of LatAm Novels; Running (for women); Building a Jenkins Plugin and a Jar (for Command Line use); Headphone Test Recordings; Causal Consistency; The Quest for Randomness; Chat Wars; Real-life Financial Co Without ACID Database...; Flexible Muscle-Based Locomotion for Bipedal Creatures; SQL Performance Explained; The Little Manual of API Design; Multiple Word Sizes; CRC - Next Steps; FizzBuzz; Update on CRCs; Decent Links / Discussion Community; Automated Reasoning About LLVM Optimizations and Undefined Behavior; A Painless Guide To CRC Error Detection Algorithms; Tests in Julia; Dave Eggers: what's so funny about peace, love and Starship?; Cello - High Level C Programming; autoreconf needs tar; Will Self Goes To Heathrow; Top 5 BioInformatics Papers; Vasovagal Response; Good Food in Vina; Chilean Drug Criminals Use Subsitution Cipher; Adrenaline; Stiglitz on the Impact of Technology; Why Not; How I Am 5; Lenovo X240 OpenSuse 13.1; NSA and GCHQ - Psychological Trolls; Finite Fields in Julia (Defining Your Own Number Type); Julian Assange; Starting Qemu on OpenSuse; Noisy GAs/TMs; Venezuela; Reinstalling GRUB with EFI; Instructions For Disabling KDE Indexing; Evolving Speakers; Changing Salt Size in Simple Crypt 3.0.0; Logarithmic Map (Moved); More Info; Words Found in Voynich Manuscript; An Inventory Of 3D Space-Filling Curves; Foxes Using Magnetic Fields To Hunt; 5 Rounds RC5 No Rotation; JP Morgan and Madoff; Ori - Secure, Distributed File System; Physical Unclonable Functions (PUFs); Prejudice on Reddit; Recursion OK; Optimizing Julia Code; Cash Handouts in Brazil; Couple Nice Music Videos; It Also Works!; Adaptive Plaintext; It Works!

© 2006-2013 Andrew Cooke (site) / post authors (content).

Calling OpenCL from Octave / Matlab

From: andrew cooke <andrew@...>

Date: Wed, 9 Dec 2009 11:42:22 -0300

[I am optimising some Matlab code but, because I don't have a Matlab
licence on my local dev machine, am using Octave.  So what follows is
a bit odd, because I am using Octave, but via the Matlab-compatible
tools.  That doesn't affect the main points, though...]

Calling OpenCL from Octave / Matlab turns out to be (at least in a
simple case) very similar to calling C.  The main issues are (1) C/C++
interop and (2) libraries.


Currently the Octave code calls a C routine which, in turn, calls the
OpenCL C++ code.  The OpenCL C++ code is compiled into a library
(called "support" below).  I used this approach because I already knew
Octave (+ Matlab) can call C, and wasn't sure about C++.  So this
moves worries about C/C++ interop to the C level (where I know how to
deal with them) rather than the Octave/Matlab level (where live might
be more complicated).  In other words (to labour the point) I am using
C as the main interface to Octave / Matlab, not C++.

So the script to build the code looks like this:

g++ -fpic -Wall
-I/home/andrew/pkg/NVIDIA_GPU_Computing_SDK/OpenCL/common/inc
-I/home/andrew/pkg/NVIDIA_GPU_Computing_SDK/shared/inc -o support.o -c
support.cpp
gcc -shared -Wl,-soname,libsupport.so -olibsupport.so support.o

gcc -o main main.c -L. -lsupport -L/usr/lib64/
-L/home/andrew/pkg/NVIDIA_GPU_Computing_SDK/OpenCL/common//lib
-L/home/andrew/pkg/NVIDIA_GPU_Computing_SDK/shared/lib/
-L/home/andrew/pkg/NVIDIA_GPU_Computing_SDK/shared/lib/linux -lOpenCL
-loclUtil -lshrutil -lstdc++

mkoctfile -v --mex singlec5.c -L. -lsupport -L/usr/lib64/
-L/home/andrew/pkg/NVIDIA_GPU_Computing_SDK/OpenCL/common//lib
-L/home/andrew/pkg/NVIDIA_GPU_Computing_SDK/shared/lib/
-L/home/andrew/pkg/NVIDIA_GPU_Computing_SDK/shared/lib/linux -lOpenCL
-loclUtil -lshrutil -lstdc++

(note the use of fpic - more on that below; also my toolkit is
installed in a non-standard location).  The "main" above is a separate
C program that calls the OpenCL routines.  Getting this working first
allowed me to fix basic errors without worrying about Octave.

The file singlec5.c is the "mex" file that handles the call from
Octave, unpacks/packs data structures and calls the OpenCl code in the
support library.


All the above is pretty normal, I think.  However, it won't work "out
of the box" because it requires that the support libraries (provided
by NVidia and used in their demos, which I based my code on) are
compiled only as ".a" (archive) libraries and not ".so" (shared
object) (this is on x86-64 Linux (OpenSuse 11.1)).

To build the libraries as shared objects, modify the following two
files so that "-fpic" is included in CXX and CC as well as LINK:

NVIDIA_GPU_Computing_SDK/OpenCL/common/common_opencl.mk
NVIDIA_GPU_Computing_SDK/shared/common.mk

Then rebuild the libraries:

cd NVIDIA_GPU_Computing_SDK/OpenCL/common/obj/release
gcc -shared -Wl,-soname,liboclUtil.so -oliboclUtil.so oclUtils.cpp.o
mv liboclUtil.so ../../../common/lib/

cd NVIDIA_GPU_Computing_SDK/shared/obj/release
gcc -shared -Wl,-soname,libshrUtil.so -olibshrUtil.so shrUtils.cpp.o
mv libshrUtil.so ../../lib/

Finally, the LD_LIBRARY_PATH should include

NVIDIA_GPU_Computing_SDK/shared/lib
NVIDIA_GPU_Computing_SDK/OpenCL/common/lib

(Paths will need to be changed as appropriate - I hope that's obvious....!)

Andrew

Extern C

From: andrew cooke <andrew@...>

Date: Wed, 9 Dec 2009 11:53:38 -0300

I should also have added, although I guess it's obvious, that the
header file for support.cpp, support.h, includes

  extern "C"

before each function declaration.  This lets C code link to the C++
routines (it removes the C++ name mangling, and may do other things I
do not understand).

Andrew

Or Simply Don't Use The Libs

From: andrew cooke <andrew@...>

Date: Wed, 16 Dec 2009 11:53:00 -0300

Hmmm.  On another machine, working with Matlab rather than Octave, and
with CentOS rather than OpenSuse, the above failed to fix things.

So I revised my code.  For now I have changed the logging to use
printf().  Apart from that, the only critical support code used was
oclLoadProgSource.  That just reads the source into memory.  Reading
files in C is tedious, but not impossible; here's a replacement:

    FILE *file;
    size_t blockAvailable = 512;
    size_t blockRead;
    char *kernelSource = NULL;
    size_t kernelLength = 0;
    size_t currentSize = 0;

    printf("Loading %s\n", kernelPath);
    file = fopen(kernelPath, "r");
    if (! file) {
        printf("Could not find kernel\n");
        err = 1;
        goto cleanup;
    }

    blockRead = blockAvailable;
    while (blockRead == blockAvailable) {
        blockAvailable *= 2;
        currentSize += blockAvailable;
        kernelSource = (char *)realloc(kernelSource, currentSize);
        blockRead = fread(kernelSource + kernelLength, sizeof(char),
                          blockAvailable, file);
        kernelLength += blockRead;
    }
    *(kernelSource + kernelLength + 1) = 0;
    printf(kernelSource);

You could add more checks (eg for realloc and fread) to handle more
errors, but the above works for now (this is very much proof of
concept code).

Andrew

More On OpenCL and Matlab Here

From: andrew cooke <andrew@...>

Date: Thu, 17 Dec 2009 19:47:47 -0300

http://www.acooke.org/cute/Experience0.html

Andrew

Comment on this post