## Latest Posts

### Compiling GCC 4.6.3 on Fedora 182013-3-14

This guide assumes a 64 bit architecture First get gcc 4.6.3

GCC 4.6.3

Extract the tar

tar -xvf gcc-4.6.3.tar.gz


Install wget (required by prequesites command), development tools and 32 bit glibc headers

yum install wget glibc-devel.i686
yum groupinstall "Development Tools"


Enter the directory, download prerequisites and run configure with prefix to set the install directory, change this to your liking

cd gcc-4.6.3/
./configure --prefix=/usr/local/gcc/4.6.3


A compilation error will result when you run make so either apply the following patch or change the lines modified by the patch

patch -p1 < {/path/to/patch/file}


### Installing cuda sdk 5.0 on fedora 182013-3-14

This guide assumes a 64 bit architecture First install gcc 4.6.3 using this guide

Compiling GCC 4.6.3

Ensure that your kernel and selinux policy are up to date

yum update


Install RPM fusion

yum localinstall --nogpgcheck http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-18.noarch.rpm http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-18.noarch.rpm


Install the nvidia graphics driver

yum install akmod-nvidia xorg-x11-drv-nvidia-libs


We need to get the cuda sdk, in this case get the version for fedora16

install prerequisites for cuda sdk

yum install glut-devel


### Thrust Reduce by Key Crashes in 64 bit2013-3-3

Source for fix

The problem seems to only happen when compiling for 64 bit architectures. I have experienced this problem and usually ended up copying data to host before doing the reduce operation.

The fix is very simple, open up:

thrust/detail/backend/cuda/reduce_by_key.inl


Find the line that states:

 typedef typename thrust::iterator_traits::difference_type  IndexType;


and change it to:

typedef  unsigned int  IndexType;


Reduce by key should now work on the device.

### Formatting Code in Xcode with Astyle2012-12-18

If youve ever wanted automatic code formatting in xcode like that in eclipse, it is entirely possible with an automator workflow and astyle.

first install astyle, if using macports:

sudo port install astyle


Then in our home directory we need to create a .astylerc file. This file defines the style for the code formatting. An example is provided below, for more info refer to the documentation.

Example .astylerc :

style=kr
brackets=attach
delete-empty-lines
keep-one-line-blocks
convert-tabs
indent=spaces=8
indent-namespaces
indent-classes
indent-cases
indent-preprocessor
break-blocks
align-pointer=name
suffix=nonekr


### Add custom compiler to Xcode2012-12-06

I do a lot of my coding on osx, and was getting annoyed that I could not pick a specific compiler from within Xcode that I had installed. Using makefiles is an option but I wanted to see if I could add a new compiler to Xcode 4. This guide was the basis for mine, tweaked for Xcode 4

Xcode 4 and gcc-4.7 will be used as the example, in theory any compiler can be used (I have not tested this). This requires that you have installed gcc-4.7 from macports (usually gcc-mp-4.7)

Open up a terminal window (Root privileges will be required for some steps!) Go to you applications folder and open up the "Xcode.app" package.

cd /Applications/Xcode.app/Contents/PlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins


Next create a copy of GCC 4.2.xcplugin and put it in the Xcode plugin path make the /Library/Application Support/Developer/Shared/Xcode/Plug-ins/ if it doesn't already exist

sudo mkdir -p /Library/Application Support/Developer/Shared/Xcode/Plug-ins/
sudo cp -r "GCC 4.2.xcplugin" "/Library/Application Support/Developer/Shared/Xcode/Plug-ins/GCC 4.7.xcplugin"


Navigate into package

cd "/Library/Application Support/Developer/Shared/Xcode/Plug-ins/GCC 4.7.xcplugin/Contents"


Next we need to convert the plist into xml as it is in binary so that we can edit it (if you'd prefer to use something other than vi, feel free)

### Output Spectrum Data From MP3 to TXT Using BASS Library2012-11-25

In order to make the fountain video I needed to take an mp3 file and get its spectrum data to an ascii file. This data would then be used as the input to the simulation.

The code below will take an "INPUT.mp3" and output to "OUTPUT.txt" the rate at which the data is collected (fps) can be controlled along with the resolution of the data (BANDS).

The code was written using the writewav.c and spectrum.c examples in the BASS sdk. The library is free for non-commercial use and is availible for Windows, Mac OS, Linux, Win64, WinCE, iOS, Android, and ARM

//Written by Hammad Mazhar
//2012 hamelot.co.uk
//

#include <stdlib.h>
#include <stdio.h>
#include "bass.h"

#ifdef _WIN32 // Windows
#include <conio.h>
#else // OSX
#include <sys/types.h>
#include <sys/time.h>
#include <termios.h>
#include <string.h>
#endif

FILE *outputdata;
char *outputname = "OUTPUT.txt";
char *input_name = "INPUT.mp3";
double fps = 60;    //how many samples should be taken per second
int BANDS = 120;    //how many "bins" to devide the spectum in (also the number of columns on each row of output)



### Generating Hexagonally Close-Packed Spheres2012-11-25

This C++ code snippit will generate a hexagonally close-packed set of spheres. It's usefull when you are simulating a block of material and want its initial rest configuration to be at its most packed state.

The code contains two functions, addHCPSheet and addHCPCube; addHCPCube creates layers of sheets using the addHCPSheet function. The input to addHCPCube is the number of particles in each dimension of the cube, the sphere radius and the global 3D position of the cube.

The code is not optimal, I usually only use it to generate particles at the begining of the simulation so the overhead incurred by division operations is not that much.

void addHCPSheet(
int gridx,       //number of particles in x direction
int gridz,       //number of particles in z direction
double height,    //height of layer
double globalx,  //global offset of sheet in x
double globalz)  //global offset of sheet in z
{
double offset = 0;
double x = 0, y = height, z = 0;
for (int i = 0; i < gridx; i++) {
for (int k = 0; k < gridz; k++) {
//need to offset alternate rows by radius
offset = (k % 2 != 0) ? radius : 0;
//x position, shifted to center
x = i * 2 * radius + offset  - gridx * 2 * radius / 2.0 + globalx;
//z position shifted to center
z = k * (sqrt(3.0) * radius)  - gridz * sqrt(3.0) * radius / 2.0 + globalz
// x, y, z contain coordinates for sphere position
}
}
}
int gridx,      //number of particles in x direction
int gridy,      //number of particles in y direction
int gridz,      //number of particles in z direction
double globalx, //global offset in x
double globaly, //global offset in y
double globalz) //global offset in z
{
double offsetx = 0, offsetz = 0, height = 0;
for (int j = 0; j < gridy; j++) {
height = j * (sqrt(3.0) * radius);
//need to offset each alternate layer by radius in both x and z direction
offsetx = offsetz = (j % 2 != 0) ? radius : 0;
}
}



### Run Gentoo on Android via Chroot2012-11-21

#### What is Chroot?

Well it's not a virtual machine or emulation, it's an honest to goodness linux install...running inside of another linux install. Inception style.

Chroot in essence is a method of running a linux distribution that is not at the standard "/" location, for example in this guide gentoo will be running in /data/local/mnt/. You then "chroot" to that directory and voila you have jumped into that linux distribution and can proceed as if you were running linux natively.

This guide will provide instructions on how to install gentoo on an android tablet. It is assumed that you are doing this on a unix system (debian, gentoo, fedora ubuntu etc) (syntax for some commands might be slightly different for different distributions

Familiarity with a linux environment is required and i assume that some blanks can be filled in, we are installing gentoo after all ;)

First I'd like to thank the people over at linuxonandroid for providing the bootscripts which I based mine off of.

## Requirements

1. Rooted android tablet with superuser (in this case i'm using a Nexus 10)
2. A compiled version of busybox-android get it here (Full credit for this goes to Stephen (Stericson) )
3. Android Terminal Emulator

## Creating the Image

All steps in this part will be performed on your linux install. we will copy this image to the device later

#### Create a linux image using dd

Configure this to the size you want, the filesystem of the sdcard partition on your device might be a limitation.

dd if=/dev/zero of=gentoo.img bs=1M count=0 seek=3072


### CUDA Syntax Highlighting in Eclipse2012-11-19

#### Enable syntax highlighting for CUDA files in Eclipse:

• Window -> Preferences -> in C/C++ -> File Types -> New
• Enter "*.cu" and select "C++ Source File"
• Repeat and enter "*.cuh" and select "C++ Header File"

#### Prevent Eclipse from complaining about global and others: In your code, either include cuda_runtime.h or add the following:

 #ifdef __CDT_PARSER__
#define __global__
#define __device__
#define __shared__
#endif


See http://forums.nvidia.com/index.php?showtopic=90943&view=findpost&p=1249657 for a similar trick related to kernel invocations, but note that its use is discouraged.

### Scientific Rendering Using Mitsuba - Physically Based Renderer2012-11-19

To render some of my simulations I have recently started using Mitsuba as a quick way to get a nice visualization. The scene description file is very well documented Here. In this post I am going to provide some example code to show how I setup a scene description, stage the simulation data to render and finally render the data.

### XML

#### Scene Definition

Mitsuba reads in all of it's data via XML files. Here is an example of a scene file. I generally use a single scene file for an entire simulation. This does mean that my camera will be static however.

Note the $file.xml which I include. This is where the simulation data is actually defined, at runtime you can define the "$file" variable to the data file for the current frame (ex: data123.xml)

<?xml version="1.0"?>
<scene version="0.4.0">
<integrator type="photonmapper">
<integer name="maxDepth" value="32"/>
</integrator>
<sensor type="perspective">
<transform name="toWorld">
<lookat target="0.000000 , 3.000000, 0.000000" origin="0.000000 , 11.000000, -30.000000" up="0.000000 , 1.000000, 0.000000"/>
</transform>
<sampler type="ldsampler"/>
<film type="ldrfilm">
<integer name="height" value="1200"/>
<integer name="width" value="1920"/>
<rfilter type="gaussian"/>
</film>
</sensor>
<include filename="\$file.xml"/>
</scene>