<- previous    index    next ->

Lecture 15, Parallelism in your GUI

How can you make your graphics run faster?
What takes the time?
Oh, 1) physics to compute new location of objects.
    2) AI or other problem solving logic
    3) rendering
Ah Ha! multiple processors, at least three, can help.

You probably already have a graphics processor.
  How much of its capability is being used
  depends on your graphics tool kit.

You may have multiple cores.
  How much they are used depends on your program.

In order to understand parallelism, you need some information
on computer architecture and operating system.

A multiple core computer is a "shared memory" system.
All cores get program and data from the same RAM and the
same hard drives and the same CD/DVD's.

Multiple cores can run separate "processes" with each process
having its own memory space. There can be inter-process
communication through the operating system. in general,
think of a process as an individual program.

Multiple cores can run one process that has multiple threads.
All threads in a process share the same address space.
Threads may communicate using memory and also by control
structures within a program.

Threads are one of the easier methods of using a multiple
core computer to speed up a single program such as your
graphics program.

Language support for threads is available to C, C++ and any
language that can call a C library by using pthreads.
Some people think this is too low level, yet you can
get maximum flexibility and maximum speed (if you can
get it working).

Java and Python have threads available. Ada calls them tasks
yet typically uses the underlying pthreads for implementation.
Threads must still have some interaction with the
operating system, because the operating system has full
control of what is running on which core.

I have shown one sample program that used Java threads,
draw3D.java. This was not for speed, but rather to have
a thread for each window. Thus the program was significantly
easier to write and debug. There was no possibility of
deadlock or data corruption.

In order to get a speed up using multiple cores, some
careful planning is required:
  Start with double buffering. The display is showing
  the previous frame while the program is computing
  a future frame.

  Assign a thread to do AI or problem solving logic. This is
  based on a previous frame and will provide the driving
  information to the physics calculations. The new data
  must be in a separate buffer, and can not overwrite data
  that may be in use.

  Assign one thread to do motion calculations, the physics.
  Data on all objects from a previous frame are in RAM.
  The new data for all objects is computed and stored in
  a separate place in RAM, ready to be used.

  Use the GPU, Graphics Processing Unit, to render, shade,
  the next frame. There is probably double buffering inside
  the GPU so that the entire newly rendered frame is
  written to the display as one image.

  These threads are scheduled and each gets its assigned
  old and new buffers. Not hard to program.

  Then, there is the asynchronous thread. The thread receiving
  user inputs. This must carefully insert the information
  from the user at the start of a AI thread and allow the
  ongoing work of other threads to continue. Here,
  synchronizing may get complex.

May demonstrate java -cp . RunThread

A simple example of Java threads, demonstrated using four windows, is RunThread.java A simple example of Python threads, with no windows, is thread_example.py The output is thread_example_py.out More complex, using barriers: barrier_test.py barrier_test_py.out

May demonstrate python worker_threads.py

Another simple example of Python threads, using a window to start threads is worker_threads.py

May demonstrate ssh -Y maya.umbc.edu ...

A not so simple example using MPI on a massively parallel computer, or as demonstrated, using 4 computers in a cluster, is w1mpi.c You do not have to memorize your physics text book, yet you should be able to look up basic equations. If all else fails, check my WEB page here.

May demonstrate frogger

My crude cut at "frogger" with mouse action at various speeds. Note logs coming out of waterfall. Water some day. No 'gators yet. Now, choice of mouse click on where frog is to jump. Thus, can test if on log or in water, take different actions. frogger.c More physics and dynamics in various "spring" files. This was a prototype for an education module for High School physics. Note drawing of spring used prior "curves and surfaces" F = k * x for spring (note heuristics) a = F/m see "physics" in code v = v + a * dt x = x + v * dt dt is delta time, the time step for each frame Note buttons show what will happen if you click. The text changes after you click. Note heavy objects should look heavier. Stronger springs should look stronger. Give the user good visual clues in your GUI. Run sequence gl,2gl,3gl, .java, db.java double buffered springgl.c spring2gl.c spring3gl.c Spring.java Springdb.java smoother spring.c Motif

May demonstrate racegl

Around the track, the physics and geometry of a race track. Note incline on curves. (See code in "build_track") Note navigation around curves. (See code in "drive") racegl.c More threads with printing time thread_more.py3 source thread_more_py3.out
    <- previous    index    next ->

Other links

Many web sites on Java GUI, AWT, Swing, etc.
Many web sites on Python wx, tk, qt, etc.

Go to top