Using OpenCL on F# via FSCL

This is my post on the F# advent calendar 2016.

As user of F# I love the language but today I’d like to talk about the point that I’ve been struggling more, how to run F# code on GPU. On Fsharp.org there’s a nice list of libraries that can run F# on GPU, but some of them are very old and specially they are not updated since long time ago.  Another problem that you can find is that there’s few documentation. For this reason I decided to write a simple example on how to use FSCL (Fsharp to OpenCL), one of the methods to run F# code on GPU.

FSCL can be installed from NuGet and since I prefer to work with scripts, the first thing that you must do it’s to reference all the library files and open FSCL

The library contains a number of functions to see which ones are your OpenCL devices and it’s useful to check before you execute any code if the OpenCL drivers are already installed and OpenCL is ready to run, so before starting we ask to the system how many OpenCL devices we have

Thus, after we have called FSCL and we know that our device is compatible with OpenCL, we can start defining constants and functions which are very similar to the standard ones

As you can see, a function ready to run in OpenCL with FSCL is very similar to one that you’ll write normally with:

  1. Attributes: ReflectedDefinition and  Kernel. ReflectedDefinition is necessary to specify it on all the FSCL functions and Kernel if it’s a Kernel. it’s not  always necessary to use the attribute kernel since nested functions are working.
  2. the parameter called WorkItemInfo that we need to pass to all our FSCL Kernel indicating the global and local size of our kernel.

In this case, both functions used properly will produce the same output, so now we need to define some arrays to operate with them and which one is the size of our kernels:

The last step is to run the kernel and compare how fast is compared with the Fsharp equivalent function and we iterate it many times to see the performance:

Here I show the CPU serial code, but it can be run in parallel with the use of Array.Parallel.iter(). The comparatives for my laptop (i7+Gforce GTX 960M) are:

FSCL:

Real: 00:00:11.043, CPU: 00:00:08.406

F#

Real: 00:03:21.406, CPU: 00:03:21.812

Using Array.Parallel.iter:

Real: 00:00:55.536, CPU: 00:04:54.671

So using the GPU with FSCL it’s been possible to accelerate the code x5 with respect to the parallel way on CPU, but it’s important to remember that since the code is converted to OpenCL, the use is not restricted to GPU and can be run in many devices, as an example, it also runs well on Intel’s Xeon-Phi.

 

Anuncios

IFSharp

Hace unos días he encontrado un proyecto interesante mediante el blog de Sergey Tyhon. Este proyecto es IFSharp en Github, y es una integración del Kernel de Fsharp en Ipython Jupyter notebook.

Según explica en la página de IFSharp,  funciona con ipython notebook 1.x y 2.x, pero es fácil encontrar en Github una ramificación (no se si es traducible de branch, pero bueno, yo traduzco) que funcione con las últimas versiones que ya se llaman Jupyter notebook. Esto se debe a que actualmente IFSharp se encuentra todavía en versión beta y por lo tanto aún le falta para llegar al producto final, pero este hecho no tiene nada que ver con su funcionalidad que es genial y merece publicidad.

Por esto, si trabajáis con F# os recomiendo probar las notebook en vez de un script, ya que la forma que podéis explicar el código siguiendo el estilo markdown es mucho más claro que los comentarios en código. Para ello lo mejor es dejaros un notebook con unos pocos ejemplos que podéis descargar aquí. Y a continuación tenéis una imagen que muestra como es una IFSharp notebook. Si alguna vez habéis utilizado ipython notebook ya sabéis como es.

2016-06-05 (1)

Ejemplo de IFSharp notebook

Si alguien quisiese muestre algo más simplemente que comente y pida, si mis conocimientos pueden ayudar, lo haré encantado y incluiré la petición en el fichero actual. Y para tener un poco más de información recomiendo este link.

Infografía Elecciones a Cortes Generales 2016

Hoy he decidido ponerme a buscar información sobre como puedo votar para estas segundas elecciones que tenemos en junio y como resido abito de forma temporal fuera de España, he pensado que quizás pueda hacerlo para votar en la embajada/consulado. Por ello he decidido buscar un poco de información en la página de mi consulado de referencia y por suerte hay un link (bastante fácil de encontrar por fortuna) sobre como lo puedes hacer para votar si estas fuera. Pero hay algo que me ha sorprendido, y es nada màs y nada menos que esta bonita infografía:

2016_JUNIO_INFOGRAFIA-ELECCIONES-JUNIO

Os voy a decir que como profano del diseño gráfico me gusta parcialmente por que es clara, pero me da rabia que falta una información vital, cuales son las fechas limites de cada paso. ¿De verdad cuesta tanto añadir la fecha limite con la descripción ?

Con ello no quiero decir que la información este muy oculta, pero tampoco me parece que este todo visible que podría estar…

Así que quien quiera votar, la fecha es clara, tiene que ir a su embajada/consulado antes del:

28 de mayo

Y rellenar la documentación pertinente que se encuentra en la web del ministerio de exteriores.

Feliz voto 🙂

 

gdb and relate topics

Since I am learning how to debug with gdb, I have summarized some manual for my own use. So here works my summary.

Some concepts look important to understand how gdb works. This post is going to be mainly based on the manual that can be find on www.dirac.org

Since the debugger needs to manage the information, there’s importance to understand the concept of MMU (Memory Management Unit)

Stack and Stack Frames: Each stack frame represents a function call, and the stack is the collection of stack frames. The stack also can be called the call stack.

small comment on how to compile a .c in order to use it with gdb: You should use the flag -g or -ggdb in order to be able to use the compiled version with gdb. so the call should be something like:

gcc -g -o output input

So, how gdb can be used? It’s simple, just open gdb with the program you want to execute (on mono programme’s, you open mono and run the .exe later). This is simply:

gdb program

And once you are inside the gdb enviroment there are many things that you can do:

  • run: it runs the program specified after gdb. arguments can be specified after run and if you call again ton run, those args are stored. In order to not use them again it’s necessary to first execute ‘set args‘.
  • list: shows the next 10 lines on the code. The syntax is list [num] or a [functionname] or you can also change to another file with list [filename]:[line] or list [filename]:[func]. You can also access to one memory point with list *. Also possible to use where to print the current line.
  • break: set a breakpoint. If you specify a number, then it breaks on the line, but you can also specify a function name or an address with *. The breakpoints can be specified on a different file as would be: break file2.c:line#. In order to see them you can write on the command line ‘info breakpoints‘ or just ‘i b’. In addition you can enable them or disable and ignore them specifying the breakpoint number. With ‘clear‘ you can eliminate the breakpoints.
  • After hitting a breakpoint you can continue until the next one
  • backtrace: it show you the stack with all the stackframes
  • step and next: execute a new step  and next executes a line. And in case you have advanced many lines, it’s always possible to know where you are with the command where.
  • frame
  • print: (also just p)with a print var you can print a variable.

Print is able to print in addition of single variables, also arrays with some options to print a single element p myArray[10] or one element + some elements after the defined p myArray[5]@10 where will print from the element 5 to 14.

In addition the type can be print. This can be done with pt as a command with pt myType and the type definition will be printed. This last point means that since it can work with definitions, gdb can print one part of the structure as print MyStruct.Name. In case you want to print a pointer, you can print the reference and dereference it and print it [print &p and print *(&p)]

 

  • Debugging a process (Already being executed)

If you execute a code out of gdb it’s also possible to debug it. This is done in the way that if you execute the program with &, then you see the jobID which allows you to access it in order to debug. Then you enter in gdb with gdb ./program JobID and you can debug it ‘on the air’. Once inside you can debug the programme in a normal way.

 

Emergencia

Hace unas semanas pude ver esta conferencia de Steven Strogatz para TED que habla de sincronización y solo puedo decir que esta muy bien. Sin estropear la conferencia, habla del fenómeno de la emergencia que sale en los sistemas complejos (y no tiene nada que ver con llamar a los bomberos). Así que os dejo con metrónomos que se sincronizan y peatones que no pueden cruzar un puente sin marchar al unísono.

Hello World!

Program hello_world  !Ahora compila

implicit none

integer :: language

print*, “Elige idioma, choose language”

print*, “Castellano=0, English=1″

read*, language

if (language.EQ.0) then

    print*, “Hola Mundo”

else if (language.EQ.1) then

    print*, “Hello World”

else

    print*, “No has entendido como funciona esto.”

end if

end program hello_world