New in Wolfram
Mathematica
8: CUDA and OpenCL Support
◄
previous
|
next
►
Software Development
Use Symbolic Graphics Primitives to Visualize Results
CUDA and OpenCL can be used for computation and then
Mathematica
can be used for visualization.
In[1]:=
X
Needs["CUDALink`"] BallBouncePattern[] := Module[{code, bb, BlockDim}, code = " #include <math.h> __global__ void bb(Real_t* v, Real_t* z, Real_t* r, int *state, \ Real_t acc, int size, int seq) { int ix = threadIdx.x + blockIdx.x*blockDim.x; int iy = threadIdx.y + blockIdx.y*blockDim.y; int i=ix*size+iy; if(ix < size && iy < size && sqrt((float)( (ix-size/2)*(ix-size/2)+(iy-size/2)*(iy-size/2) ) \ ) >=(float)seq ) { if(v[i]<0) { v[i]=0; state[i]=1; } if(z[i]<0) { z[i]=0; state[i]=-1; } v[i]+=state[i]*acc*r[i]/.25; z[i]-=state[i]*v[i]; } }"; BlockDim = {8, 8}; bb = CUDAFunctionLoad[code, "bb", {{"Float"}, {"Float"}, {"Float", _, "Input"}, {_Integer}, _Real, _Integer, _Integer}, BlockDim, "TargetPrecision" -> "Single"]; Mouseover[ Graphics[{LightGray, Circle[], Inset[Style["Bring Mouse Here", Bold, Blue]]}], BallBounceEffect[bb] ] ] BallBounceEffect[bb1_] := Module[{tsize, fsize, z, r1, r, v, acc, device, state, za, ra, va, sa, code, BlockDim, GridDim, res, res1, res2, vc, s}, tsize = 101; fsize = 80; z = Table[fsize - 2 + RandomReal[2], {i, tsize*tsize}]; r = Table[.25, {i, tsize*tsize}]; v = ConstantArray[0.0, tsize*tsize]; acc = 0.2; s = Ceiling[tsize/Sqrt[2]]; device = Automatic; state = ConstantArray[1, tsize*tsize]; vc = Map[ColorData["BrightBands"], Range[0, 1, 1/(tsize^2 - 1)]] //. RGBColor -> List; Graphics3D[{AbsolutePointSize[0], Point[Dynamic[Refresh[ {v, z, state} = bb1[v, z, r, state, acc, tsize, s--, {tsize, tsize}]; Flatten[ Table[{i, j, z[[(i - 1)*tsize + j]]}, {i, tsize}, {j, tsize}], 1], UpdateInterval -> 0]], VertexColors -> vc], Sphere[{tsize/2, tsize/2, -10}, .05], Sphere[{tsize/2, tsize/2, fsize}, .05], {Black, Polygon[{{tsize, 0, 0}, {tsize, tsize, 0}, {0, tsize, 0}, {0, 0, 0}}]} }, Boxed -> False] ]
In[2]:=
X
BallBouncePattern[]
Play Animation
»
Stop Animation
»