Skip to content

Commit

Permalink
OpenCL works
Browse files Browse the repository at this point in the history
  • Loading branch information
cirosantilli committed Nov 2, 2015
1 parent 6d9e6ca commit 8087bb3
Show file tree
Hide file tree
Showing 32 changed files with 407 additions and 107 deletions.
4 changes: 3 additions & 1 deletion Makefile_many
Expand Up @@ -108,9 +108,11 @@ profile: clean set_profile_flags all run
cd '$(OUT_DIR)' && gprof -b '$(RUN_BNAME)' gmon.out | tee "$(RUN_BNAME).profile_out" | less

run: all
@echo
cd $(OUT_DIR) && ./$(RUN_BNAME)

time: all
cd $(OUT_DIR) && time -p ./$(RUN_BNAME)

set_profile_flags:
$(eval PROFILE_FLAGS := -p -pg)
$(eval PROFILE_DEFINE := -DPROFILE)
Expand Down
2 changes: 1 addition & 1 deletion Makefile_one
Expand Up @@ -74,7 +74,7 @@ clean:
if [ ! '$(OUT_DIR)' = './' ]; then \
rm -rf '$(OUT_DIR)' ;\
else \
rm -f *'$(OBJECT_EXT)' *'$(OUT_EXT)' *'$(TMP_EXT)' ;\
rm -f *'$(OBJ_EXT)' *'$(OUT_EXT)' *'$(TMP_EXT)' ;\
fi

debug: clean set_debug_flags all
Expand Down
1 change: 1 addition & 0 deletions c/README.md
Expand Up @@ -42,6 +42,7 @@
1. [_Static_assert](static_assert.c)
1. [Pointer](pointer.c)
1. [void pointer](void_pointer.c)
1. [Pointer to int typecast](pointer_to_int.c)
1. [NULL](null.c)
1. Branching
1. [if](if.c)
Expand Down
21 changes: 20 additions & 1 deletion c/inttypes_h.c
@@ -1,4 +1,5 @@
/*# PRIxPTR
/*
# PRIxPTR
*/

#include "common.h"
Expand Down Expand Up @@ -53,7 +54,25 @@ int main() {
# Fixed size integer printf format
# PRId16
# PRId32
# PRId64
Why not modify printf instead of adding those new macros?
http://stackoverflow.com/questions/1183679/why-werent-new-bit-width-specific-printf-format-option-strings-adoped-as-pa
# PRIu32
Unsigned versions.
*/
{
printf("PRId16 = %s\n", PRId16);
printf("PRId32 = %s\n", PRId32);
printf("PRId64 = %s\n", PRId64);

/* Samplel usage:*/
printf("%" PRIx32 "\n", (uint32_t)0xFFFFFFFF);
}
#endif
}
4 changes: 2 additions & 2 deletions c/operator.c
Expand Up @@ -95,8 +95,8 @@ int main() {
C first casts the integer type to a floating point type, then does
the floating point operation.
Division by `0` leads to different problems, which are also different
on the floating point and integer cases.
Division by `0` is undefined behaviour. On Linux it raises SIGFPE.
But note that handling the SIGFPE returns to just before the division. TODO check + example.
# INT_MIN / -1
Expand Down
4 changes: 2 additions & 2 deletions c/pointer.c
@@ -1,5 +1,5 @@
/*
# pointer
# Pointer
Pointers contain addresses of variables instead of the value.
Expand Down Expand Up @@ -38,7 +38,7 @@ int main() {
float *fp = (float *)&i;

/*
# single line multiple pointer declaration
# Single line multiple pointer declaration
You must put an asterisk for each pointer, or they are not taken to be pointers!
Expand Down
15 changes: 15 additions & 0 deletions c/pointer_to_int.c
@@ -0,0 +1,15 @@
/*
# Pointer to int typecasts
Implementation defined.
http://stackoverflow.com/questions/7677919/unexpected-sign-extension-of-int32-or-32bit-pointer-when-converted-to-uint64
TODO convert forward and back guaranteed equal?
*/

#include "common.h"

int main() {
return EXIT_SUCCESS;
}
3 changes: 2 additions & 1 deletion c/signal.c
Expand Up @@ -45,7 +45,8 @@ or specific operating systems
Integer division by 1 / 0 may generate a `SIGFPE`.
TODO is 1 / 0 guaranteed to generate a `SIGFPE`?
1 / 0 is undefined behaviour. On x86, it generates a Divide Error,
which Linux delivers as a SIGFPE.
TODO how to deal with it? it just keeps coming back time after time.
Expand Down
16 changes: 10 additions & 6 deletions c/stdio_h.c
Expand Up @@ -95,6 +95,8 @@ Returns a pointer to the start of that array.
In case of any error, returns NULL.
The entire file must fit into the memory avilable to the program.
http://stackoverflow.com/questions/174531/easiest-way-to-get-files-contents-in-c
*/
char *file_read(char *path) {
FILE *fp;
Expand Down Expand Up @@ -596,7 +598,7 @@ int main() {
- return `NULL` and set `errno`.
# Text IO vs # Binary IO
# Text IO vs Binary IO
# Text vs binary for numerical types
Expand Down Expand Up @@ -845,7 +847,7 @@ int main() {
*/
}

/* # applications */
/* # Applications */
{
{
char path[] = TMPFILE("str_file");
Expand Down Expand Up @@ -879,13 +881,15 @@ int main() {
}

/*
Process a file #linewise.
# Linewise file processing
Process a file linewise.
Allows one to read files larger than RAM, suppposing that each line is smaller than RAM.
Allows one to read files larger than RAM, suppposing that each line is smaller than RAM.
glibc and C++ stdlib offer the `getline` function which does it.
glibc and C++ stdlib offer the `getline` function which does it.
There does not seem to be such a function in C! <http://stackoverflow.com/questions/3501338/c-read-file-line-by-line>
There does not seem to be such a function in C! http://stackoverflow.com/questions/3501338/c-read-file-line-by-line
*/
{
FILE* fp;
Expand Down
2 changes: 2 additions & 0 deletions c/undefined-behaviour.md
Expand Up @@ -33,6 +33,8 @@ Examples that explain both:

Each implementation must document what it does but setting a fixed parameter.

GCC has a list of its choices at: <https://gcc.gnu.org/onlinedocs/gcc/C-Implementation.html>

E.g.: `sizeof(int)`

The standard may put constraints on what is conforming.
Expand Down
23 changes: 9 additions & 14 deletions c/void_pointer.c
Expand Up @@ -51,27 +51,22 @@ int main() {
This means that `sizeof(void)` is not possible, nor are pointer arithmetic operations.
*/
{
/* it is however possible to get the size of a `void*` */
/* It is however possible to get the size of a `void*`. */
printf("sizeof (void*) = %zu\n", sizeof(void*));

printf("sizeof (void*) = %zu\n", sizeof(void*));

/* ERROR: invalid application of sizeof to void type */

/*vp = vp + 1;*/
/* ERROR: invalid application of sizeof to void type. */
/*vp = vp + 1;*/
}

/* int* to void*: */

vp = (void*)&i;
vp = (void*)&i;

/* void* to int*: */
ip = (int*)vp;

ip = (int*)vp;

/* void* to int. GCC 4.8: cast from pointer to int of different size: */
/* `-Wpointer-to-int-cast */

/*i = (int)vp;*/
/* void* to int. */
/* WARN GCC 4.8: cast from pointer to int of different size: -Wpointer-to-int-cast */
/*i = (int)vp;*/

/*
Typecast to the bad type.
Expand Down
12 changes: 12 additions & 0 deletions cpp/placement-new.cpp
@@ -0,0 +1,12 @@
/*
# Placement new
TODO
http://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new
*/

#include "common.hpp"

int main() {
}
4 changes: 2 additions & 2 deletions gcc/128_bit_int.c
Expand Up @@ -7,10 +7,10 @@
https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/_005f_005fint128.html
Where it is supported:
- http://stackoverflow.com/questions/3329541/does-gcc-support-128-bit-int-on-amd64
- http://stackoverflow.com/questions/21886985/what-gcc-versions-support-the-int128-intrinsic-type
No hardware support it seems.
*/

#include "common.h"
Expand Down
2 changes: 2 additions & 0 deletions gdb/count_infinite.c
@@ -1,5 +1,7 @@
/*
To break interactively in the infinite loop, enter Ctrl + C.
- http://stackoverflow.com/questions/8702658/gdb-how-do-i-pause-during-loop-execution
*/

#include <stdio.h>
Expand Down
1 change: 1 addition & 0 deletions opencl/Makefile
9 changes: 9 additions & 0 deletions opencl/Makefile.bak
@@ -0,0 +1,9 @@
.POSIX:

.PHONY: all clean

all:
gcc -std=c89 -o main.out main.c -lOpenCL

clean:
rm hello
1 change: 1 addition & 0 deletions opencl/Makefile_params
@@ -0,0 +1 @@
LIBS := -lm -pthread -lOpenCL
25 changes: 10 additions & 15 deletions opencl/README.md
@@ -1,17 +1,12 @@
# OpenCL

OpenCL is an open standard language which models CPU GPU computing.

It is similar to C, containing some extra keywords to model the CPU GPU world, and leaving out a few C features which GPUs cannot yet do very well.

As of 2013 its major concurrent is CUDA, which NVIDIA proprietary. Current NVIDIA do support both CUDA and OpenCL.

The OpenCL standard in maintained by the Khronos Group, the same guys who maintain OpenGL. They are an industry consortium.

OpenCL, like any other language has versions. As of 2013 the latest version is OpenCL 2.0 released in preview (unstable) as of Jul 2013.

## Install

The first step is to get you GPU working. Good luck on that, specially if you are on Linux =)

Next, you will need an SDK developed by the GPU vendor. There is no fixed standard assembly language for GPUs, in part because GPUs are changing very fast, so vendors do not want to get tied down to an interface. Therefore, the only way to go is to pass through the vendor SDK which should implement OpenCL.
1. [Getting started](getting-started.md)
1. Examples
1. [min](min.c)
1. [hello_world](hello_world.c)
1. Theory
1. [Introduction](introduction.md)
1. [Concepts](concepts.md)
1. [clinfo](clinfo.md)
1. [Bibliography](bibliography.md)
1. [TODO](TODO.md)
3 changes: 3 additions & 0 deletions opencl/TODO.md
@@ -0,0 +1,3 @@
# TODO

1. Compare speeds of `CL_DEVICE_TYPE_GPU` and `CL_DEVICE_TYPE_CPU`.
6 changes: 6 additions & 0 deletions opencl/bibliography.md
@@ -0,0 +1,6 @@
# Bibliography

- Khronos standards: <https://www.khronos.org/registry/cl/>
- <http://developer.amd.com/tools-and-sdks/opencl-zone/opencl-resources/>
- <http://www.fixstars.com/en/opencl/book/OpenCLProgrammingBook>
- The specifications of your hardware, e.g. <http://www.nvidia.com/object/nvs_techspecs.html>
5 changes: 5 additions & 0 deletions opencl/clinfo.md
@@ -0,0 +1,5 @@
# clinfo

`glxinfo` for OpenCL:

sudo clinfo
15 changes: 15 additions & 0 deletions opencl/concepts.md
@@ -0,0 +1,15 @@
# Concepts

## Platform

TODO what is a platform?

<http://stackoverflow.com/questions/3444664/does-any-opencl-host-have-more-than-one-platform>

## Compute unit

TODO vs core? Each compute unit has a number of cores it seems:

## Work group

TODO
19 changes: 19 additions & 0 deletions opencl/getting-started.md
@@ -0,0 +1,19 @@
# Getting started

## NVIDIA

On Ubuntu 15.10 with an NVIDIA NVS 5400M, Lenovo T430:

sudo apt-get install nvidia-352 nvidia-352-dev nvidia-prime
sudo ln -s /usr/include/nvidia-352/GL /usr/local/include
sudo ln -s /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 /usr/local/lib/libOpenCL.so

Then compile with:

gcc -o main main.c -lOpenCL

Notes:

- find your GPU model: <http://askubuntu.com/questions/72766/how-do-i-find-out-the-model-of-my-graphics-card>
- test that the driver is working: <http://askubuntu.com/questions/68028/how-do-i-check-if-ubuntu-is-using-my-nvidia-graphics-card>
- do not install the `nvidia-current` package. It is old. Either `apt-cache search nvidia` and get the latest one, or use `software-properties-gtk` "Additional Drivers" tab.

0 comments on commit 8087bb3

Please sign in to comment.