|
Example: Palindromes |
Top Previous Next |
|
This example shows how Diamond can be used to create a 'supercomputer type' application to deal with a problem that is compute-intensive and can be speeded up by sharing the work across several processors.
What is a Palindrome? If you take a number, say 194, and add it to the number you get by reversing the digits (491): you get a new number (685). Repeat this process several times:
194 + 491 ====== 685 + 586 ====== 1271 + 1721 ====== 2992
This final number is a palindrome: it reads the same from right to left as from left to right.
Not all starting numbers seem to generate palindromes; a few simply continue to get bigger and bigger. This application tries this process on all the numbers in the range 1—999 and prints out those that grow to 5000 digits in length without becoming palindromic.
Description of the Application The application is based on two tasks. Task driver splits the total range of numbers to be checked into sub-ranges. The ranges are sent sequentially to a worker task paltask which returns a list of the numbers in the range that are not palindromic.
In its initial configuration the example has one instance of each task placed on the same DSP as shown below.
The driver task is written to detect how many paltask tasks are connected to it and it automatically distributes the ranges to be processed over those tasks.
It is possible to duplicate task paltask several times to use as many DSPs as possible. For example, the figure below shows the application configured to use three DSPs.
Running the Example Make sure you have set the default processor type corresponding to the hardware you have as explained here.
You can build the application by typing Ctrl+B and then execute it by clicking run.
To start with, just build and run the program as it is, and note the time taken.
Now create a new DSP processor called dsp2. Then duplicate paltask on dsp2 by dragging it with Ctrl+Shift held down and dropping it on dsp2.
This new task needs to be connected to the driver task. We start by creating two new ports on driver for these connections.
Double-click the task driver in dsp1. Click Add under Output Ports and click Finish. Click Add under Input ports and click Finish. We now have the two extra ports.
Double-click Connections in the Diamond source view. Expand all the tasks in the Source and Destination areas by clicking + by each name.
You should see four unnamed ports. Click the driver port under Source then double-click the paltask port under Destination. Click the driver port under Destination then double-click the paltask port under Source.
You can now build and run the application and note the time it takes.
You can repeat the procedure to a task on all the DSPs available on your board.
Prerequisites You need the following components to be able to run this example:
|