Andrew Cooke | Contents | Latest | RSS | 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

Choochoo Training Diary

Last 100 entries

Surprise Paradox; [Books] Good Author List; [Computing] Efficient queries with grouping in Postgres; [Computing] Automatic Wake (Linux); [Computing] AWS CDK Aspects in Go; [Bike] Adidas Gravel Shoes; [Computing, Horror] Biological Chips; [Books] Weird Lit Recs; [Covid] Extended SIR Models; [Art] York-based Printmaker; [Physics] Quantum Transitions are not Instantaneous; [Computing] AI and Drum Machines; [Computing] Probabilities, Stopping Times, Martingales; bpftrace Intro Article; [Computing] Starlab Systems - Linux Laptops; [Computing] Extended Berkeley Packet Filter; [Green] Mainspring Linear Generator; Better Approach; Rummikub Solver; Chilean Poetry; Felicitations - Empowerment Grant; [Bike] Fixing Spyre Brakes (That Need Constant Adjustment); [Computing, Music] Raspberry Pi Media (Audio) Streamer; [Computing] Amazing Hack To Embed DSL In Python; [Bike] Ruta Del Condor (El Alfalfal); [Bike] Estimating Power On Climbs; [Computing] Applying Azure B2C Authentication To Function Apps; [Bike] Gearing On The Back Of An Envelope; [Computing] Okular and Postscript in OpenSuse; There's a fix!; [Computing] Fail2Ban on OpenSuse Leap 15.3 (NFTables); [Cycling, Computing] Power Calculation and Brakes; [Hardware, Computing] Amazing Pockit Computer; Bullying; How I Am - 3 Years Post Accident, 8+ Years With MS; [USA Politics] In America's Uncivil War Republicans Are The Aggressors; [Programming] Selenium and Python; Better Walking Data; [Bike] How Fast Before Walking More Efficient Than Cycling?; [COVID] Coronavirus And Cycling; [Programming] Docker on OpenSuse; Cadence v Speed; [Bike] Gearing For Real Cyclists; [Programming] React plotting - visx; [Programming] React Leaflet; AliExpress Independent Sellers; Applebaum - Twilight of Democracy; [Politics] Back + US Elections; [Programming,Exercise] Simple Timer Script; [News] 2019: The year revolt went global; [Politics] The world's most-surveilled cities; [Bike] Hope Freehub; [Restaurant] Mama Chau's (Chinese, Providencia); [Politics] Brexit Podcast; [Diary] Pneumonia; [Politics] Britain's Reichstag Fire moment; install cairo; [Programming] GCC Sanitizer Flags; [GPU, Programming] Per-Thread Program Counters; My Bike Accident - Looking Back One Year; [Python] Geographic heights are incredibly easy!; [Cooking] Cookie Recipe; Efficient, Simple, Directed Maximisation of Noisy Function; And for argparse; Bash Completion in Python; [Computing] Configuring Github Jekyll Locally; [Maths, Link] The Napkin Project; You can Masquerade in Firewalld; [Bike] Servicing Budget (Spring) Forks; [Crypto] CIA Internet Comms Failure; [Python] Cute Rate Limiting API; [Causality] Judea Pearl Lecture; [Security, Computing] Chinese Hardware Hack Of Supermicro Boards; SQLAlchemy Joined Table Inheritance and Delete Cascade; [Translation] The Club; [Computing] Super Potato Bruh; [Computing] Extending Jupyter; Further HRM Details; [Computing, Bike] Activities in ch2; [Books, Link] Modern Japanese Lit; What ended up there; [Link, Book] Logic Book; Update - Garmin Express / Connect; Garmin Forerunner 35 v 230; [Link, Politics, Internet] Government Trolls; [Link, Politics] Why identity politics benefits the right more than the left; SSH Forwarding; A Specification For Repeating Events; A Fight for the Soul of Science; [Science, Book, Link] Lost In Math; OpenSuse Leap 15 Network Fixes; Update; [Book] Galileo's Middle Finger; [Bike] Chinese Carbon Rims; [Bike] Servicing Shimano XT Front Hub HB-M8010; [Bike] Aliexpress Cycling Tops; [Computing] Change to ssh handling of multiple identities?; [Bike] Endura Hummvee Lite II; [Computing] Marble Based Logic; [Link, Politics] Sanity Check For Nuclear Launch; [Link, Science] Entropy and Life

© 2006-2017 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

OpenCL and MATLAB - Amazing BLog. Help Needed.

From: Anushri Pampari <p.anushri25@...>

Date: Mon, 7 Jul 2014 05:17:17 +0530

Hello Andrew Cooke,

I am an undergraduate student working at Rice university. I have come
across your blog, Mex and OpenCL and I felt it very useful. Thank you so
much

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

I am currently running across a problem since a week. I dont know what the
issue could be. Hope you could help me. Thank you in advance.

So here is brief description of the issue.

I am trying to use OpenCL and Mex code. The Mex part of the code reads a
matlab dat file and the opencl code further runs the code on Intel CPU as
of now, though future plan would be to run it on a GPU. The Intel PLatform
is being recognized on just a normal OPENCL code ( without mex). But on
using the OPenCL with Mex, OpenCl fails to recognize the the Intel Platform
( platforms found 0).. I was wondering if you could help me through this. I
would be really grateful to you.

Thank you. Looking forward for your reply.

.Anusri
Third year Undergraduate.

Comment on this post