Here is the first problem to illustrate a “learn/translate(C)uda code to OpenCL code” exercise.
A 30x30 grid of squares contains 900 fleas, initially one flea per square.When a bell is rung, each flea jumps to an adjacent square at random (usually 4 possibilities, except for fleas on the edge of the grid or at the corners).What is the expected number of unoccupied squares after 50 rings of the bell?Give your answer rounded to six decimal places.
There are a few different ways of pursuing this problem.
- a one-dimensional (linear) array of count variables where you perform math to find special cases depending on the index you are on.
- quick and cheap.
- alot of math(but cheap math) per move|iteration (?)
- [C programmer | math guy] response.
- A two dimensional array, where the open choices are dependent on what indicies (edge cases) and each spot in the array has a count variable.
- easier mapping of concept to code.
- you can translate to option 1 later.
- a typical academic response.
- Create a struct that has 4 pointers and a count count variable. Put these into the array.
- Object oriented response
- Ease to iterate (less work on the array)
- easy operations per struct: find a random number between 1 and 4 and travel along a ptr that is not null
- EXPENSIVE
- Redundant with C (a slot in an array in C points to the next slot anyway)
- Off my head: and array of ‘fleas’ with ptr|index of occupied address|slot: the slot in the array holds the address the flea is on.
- need two arrays?: one for ‘fleas’, one for counter of each spot on the ‘board’
- easy to iterate thru flea array
- Do math similar to option (1) to find the possible moves
The problems:
- The count variable : are each of the fleas unique?
- if so, options 1-3 will not work unless the ‘counter variable’ is itself an array of pointers
- How do the fleas move instantaneously without double [counting|moving] a flea
- Can you use something like relative frequency to find the open number of squares? Or what other probability forms can you use?
Something like 4 would work without an array to hold the model of the board until the end. This would make the problem even more ‘parallelizable’ as you could give each ‘flea’ its own thread and tell it to move 50 times. Then [count()the occupied spaces |move the array back out to the CPU and count()].