Channels ▼
RSS

CxC: C for Parallel Computing


CxC: C for Parallel Computing

Effective parallel computing has, over the years, been one of the "holy grails" of the computer science community. There have been lots of approaches tried, both in hardware (think Connection Machine, for example) and in software (think Linda,Erlang, and Jini/JavaSpaces).

Engineered Intelligence Corporation has released a C-based parallel programming language called CxC (pronounced "C by C"). According to Matt Oberdorfer, president of EI, what the company is trying to do with CxC is to make parallel programming available to scientists and engineers who only know their discipline and the C language, and enabled them to develop on a Windows PC. Once they get their code running on a PC—even a laptop—they can move the same code to a symmetric multi-processor (SMP) Windows machine or to a Linux or Unix cluster, and do their production runs really fast.

Under the hood, CxC's runtime environment automatically maps the virtual processors you define in your CxC program to the real processors in the current configuration. In addition, CxC automatically handles the communications between nodes in the most efficient way possible: shared memory between processors on the same SMP box, or message passing between separate boxes in a cluster connected by a high-speed network. The programmer never has to worry about the real topology, or whether to use shared memory or message passing for coordination.

The CxC Language

I downloaded the CxC compiler and runtime for Windows from the Engineered Intelligence site, and installed it on my fastest development box, a single-processor 2.4-GHz Pentium 4 with 750 MB of RAM. What you download is a 30-day trial version; if you download the CxC compiler, you don't need to bother with the Grid Wars download. They're currently almost the same, but in the future the CxC package will have more platform support.

I would have liked to try CxC on a multi-CPU box, but the only ones to which I have access are production servers. "Why is the database slow? Sorry, Martin's testing a new warrior for Grid Wars." There's no way I could get away with that, not even if I wanted to work on it at 4 AM.

I was fairly impressed with how clean the CxC language is, and how much it does with just a few extensions to C. You start by specifying a virtual parallel computer consisting of multiple array controllers and parallel processing units. This example is from a parallel program to compute the value of Pi by numerically integrating with 1000 intervals:

////////////// controller and unit //////////////////////
define NUM_PPUS 1000
define CONST_A 0.5
controller ZeroToOne // 1000 intervals between 0 and 1
{
double Interval; // result for each interval
unit IntervalUnit[NUM_PPUS]; // one unit for each interval
}
controller ComputePI // Controller for result unit
{
unit PIUnit[1]; // Unit where PI value is computer
}

Then you define the connections between the nodes. In this case, one master unit for the result talks to all 1000 interval units:

//////////// topology /////////////////////////////////
topology IntervalLink // link to each IntervalUnit to
{ 			    // to PIUnit
PIUnit[0] -> IntervalUnit[*];
}

Finally you define a program for each unit:

main PI
{
////////////////////////////////////////////////////////////
// ZeroToOne: compute value for sub-interval
////////////////////////////////////////////////////////////
program ZeroToOne
 {
const long NumOfIntervals = NUM_PPUS;
double x; // temporary variable
// compute value for my part of the interval
x = (id() + CONST_A) / NumOfIntervals;
Interval = (4.0 / (1.0 + x * x));
// wait until all ZeroToOne ppu have finished
barrier;
 }
////////////////////////////////////////////////////////////
// ComputePI: distibute sub-intervals to ZeroToOne ppus
// gather sub-results and compute PI
////////////////////////////////////////////////////////////
program ComputePI
 {
const long NumOfIntervals = NUM_PPUS;
// compute the sub-values in 1000 parallel processors barrier;
// retrieve the results from the ZeroToOne PPUs
Val.fanin( Interval );
println("PI Result: " , sum(Val)/NumOfIntervals );
 }
}

As you might guess, the barrier statement synchronizes all the units, so what happens here is that all the interval-calculating nodes run program ZeroToOne and hit the barrier. Then the master unit running ComputePI gathers (fans in) the Interval variables from all the ZeroToOne controllers, and does the numerical integration.

Embedded Parallel Processing

The Pi calculation program in pure CxC. CxC has an array for input called In[] that is owned by the calling program, and another array for output called Out[].

The calling program links to CxC using a runtime library, cxcstat. It dynamically loads a compiled CxC program using the ei_InitMachine library function. This code is from a sample called Rain:

char str[200];
// in case there is a CxC exe loaded close it
ei_CloseMachine();
// load new CxC executable
if(!ei_InitMachine(fd.m_ofn.lpstrFile))
MessageBox("Could not load CxC executable",
"Load Error", MB_ICONSTOP);
else
// check if the number of unit in the
// CxC executable matches the battle field size
if( ei_GetNumOfUnit() -1 != X_DIM * Y_DIM)
{
sprintf(str, "The Number Of CxC units do not match");
MessageBox(str, "Matching Error", MB_ICONSTOP);
// close loaded file again - no match
ei_CloseMachine();
}
else
{
// successful load...
}

Once the CxC program is loaded, you can pass it data, run parallel steps, and retrieve the data with the ei_SetUnitArray, ei_SetControllerArray, ei_ComputeMachine, and ei_GetUnitArray functions. Overall, it's about as simple as it can be and still do the job.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video