tags:

views:

273

answers:

7

if I write:

a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;

I think (maybe am wrong) that the CPU will go line by line for example:

CPU says:
let me assign a to 0
then let me assign b to 0
then let me assign c to 0
then let me assign d to 0
etc...

I was just wondering if there is a processor that can change variables simultaneously...?

Thanks... Y_Y

+1  A: 

I think you might be a little confusing. You can't even achieve that using threads. What do you mean by a "call" exactly? A single line of code? That first line is just a convenience offered during declaration in many C like languages.

I mean, you could create some sort of function that takes a list of variables by reference and then a list of values and then you could just call that function. I'm not sure what you are trying to accomplish exactly.

But no, there isn't a single statement without using functions in most C like languages (you didn't specify a language) that would do that except if you want all the variables to have the same value(i.e. i = j = k = 0;).

BobbyShaftoe
+4  A: 

Unless the assignment is happening inside the database or some other transaction-enabled software, the two code examples you gave will most likely will result in 100% identical instructions being executed by the CPU.

E.g. they will be turned into the same exact sequence of op codes by the compiler/interpreter.

UPDATE:

  1. Just to be clear, as others have noted, a = b = c = 0; is merely C's (and other languages) syntactic sugar that in reality translates to:

    c = 0;
    b = c;
    a = b;
    
  2. There MAY be languages/frameworks where the 3 assignments are "atomic" in a sense of being executed all at once as a transaction, if that is what you meant by "1 call". the most obvious example is SQL where saying UPDATE table SET a=0,b=0,c=0 ... will, indeed, not only guarantee that the 3 updates happen as one "call", but are a transaction - either all are updated or none.

DVK
+5  A: 

What you have there is a line of code, not a method call.

EDIT: In response to your edited question...what you would need for that is a vector processor (discussed more below). The idea, as I remember it, is that you have a series of values, and you perform the same operations, repeatedly, in parallel, on all of them at the same time. They're typically used for large-scale math/physics, simulation, and graphics type stuff (including PC/console 3D graphics, I think).

If I remember correctly, in C, you can stick multiple statements on one "line of code", like so:

int a, b, c;
a = 1, b = 2, c = 3;

However, this is no different than writing:

int a, b, c;
a = 1; b = 2; c = 3;

which in turn is identical to

int a, b, c;
a = 1;
b = 2;
c = 3;

and all of these translate to the same thing in the compiler. So while one of them only uses one syntactical "statement", and two of them are written on a single line, they are all identical except for the syntax.

There are languages that can generate more than one value with a single call to one of the normal, basic elements of the language; for example, in Lisp you can use maphash to run the same function on every element of a hash table, and if the function writes back to the hash table again, then one "line of code" is writing many values. But it's a meaningless measure, because that one line of code is running a lot of stuff under the hood.

Probably a better example is certain parallel programming models. Vector processors are designed to allow you to run one instruction on (4/8/16/N) different data elements simultaneously and in parallel; you could meaningfully say that it's doing what you're asking about, but then you'd need to learn how to code in assembly for vector machines. Also, some parallel programming models assume that you have one parallel piece of code that runs in N threads simultaneously, starting from N different sets of input data and calculating correct output for each of them. Any implementation of the model is supposed to be able to guarantee that the different executions "come together" at particular points in the code, where everything syncs up and they can read other processes' data and act on it.

So, er, yeah, this can be a very simple question or a very deep one, depending on how far down the rabbit hole you want to go.

jprete
make sense... yeah... I was playing with physics and C and that question came up cause sometimes there are more than 50 objects that needs to be constantly updated (gravity, friction, etc...) and it would be cool if one single process would change all these properties.
Y_Y
Well, it's important to remember that, if you want to update 50 items simultaneously, you need to have 50 pieces of hardware doing that update. Even if the language pretends that you're doing it in one "command", so to speak, it might be just translating it into 50 instructions that are done one at a time. That's what happens in the Lisp case.
jprete
SSE and AltiVec does operations on multiple data items within a single instruction
swegi
@Swegi, that's good to know. One thing that's important for the context of this question is how a programmer in, for example, C++, can access said machine instructions. Will the compiler line it up if it makes sense? What kind of code is likely to get optimized that way? Or does the programmer need to inline some assembly to do it?
jprete
+1  A: 

Why not place the values in an array and use memset, or the ZeroMemory macro (which uses memset internally.

Raul Agrait
this is a good point. On some machines, memset() may actually use the DMA controller or some other special way of accessing memory to greatly speed up the zeroing. Still not likely to happen in parallel though.
rmeador
+1  A: 

Not perhaps a CPU, but this can easily be done with an FPGA.

For example, there is a C-like language called Handel C which is used for FPGA development. Because the code is translated to hardware, true parallelism at the statement level is possible.

Handle C has par and seq constructs which determine whether statements occur in parallel or sequentially. The execution time of a par block is the execution time of the slowest statement (or nested block or call), and variable assignments take 1 clock cycle so:

// Three assignments in one clock
par
{
   a = 1;
   b = 2;
   c = 3;
}

// Three assignments in three clocks
seq
{
   a = 1;
   b = 2;
   c = 3;
}

An interesting feature of this is if for example you have:

par
{
  a++ ;
  b = a ;
}

The value assigned to b is the value of a before the par block, because they happen simultaneously (and the order of the statements is irrelevant). Using this feature allows you to implement pipelined execution so that complex algorithms requiring multiple clock cycles can generate a result on every clock cycle (albeit delayed by the pipeline length).

Clifford
A: 

If you really want to know what's happening at the hardware level, that is a very deep topic. You are correct that the assignments (most likely) do not happen simultaneously. Even worse, they may not happen in the order you originally specified.

The compiler and hardware will guarantee that from the point of view of a single thread, the code will act as if it is executed sequentially. But in order to make the code execute faster they are allowed to break this illusion for other threads. This is especially the case in a multi-CPU or multi-core environment.

Alternatively, if what you are really interested in is how to speed up your code: first learn to use a profiler and find out how to measure where your code is slow. I guarantee it will not be where you expect it. Then, for speeding up your physics simulation code, start reading up on the vector intrinsics that your compiler most likely supplies for your CPU. You don't need to learn how to program in assembly language.

Paul Du Bois
+1  A: 

While not strictly related to the code above, which is a mere human-readable representation of instructions that can be translated to any machine code so long as the translation maintains the defined semantics of the specific language and environment ...

It is possible that a smart compiler could translate: int8 c = 1, c = 2; into a single int16 assignment (perhaps storing both values in a single register) if it was written to do so. On the other hand, int16 d = 4; may take multiple cycles (read: be non-atomic) on 8-bit hardware. Go figure.

Yes, certain CPUs can change two variables (multiple storage slots) in an atomic ("simultaneous") operation.

The most common of these provide the basis for hardware-safe synchronization.

CMPXCNG - Compare and Exchange

CMPSWP - Compare and Swap

In the sake of optimization of the years (even exlcuding systems designed for vectorized processing and the like), I would be surprised to not find more specialized instructions. Also, while even further from simple assignments, context switches and register file swaps may also be done atomically.

pst
This is the first correct answer I've seen here. Everyone else is picking on the C syntax and missing the atomicity constraint. The core point is that you need to look up what particular atomic operations your CPU supports (x86-64 CPUS can do it all the way up to 128 bits), write your atomic update in assembly, and then map your C variables into that memory using a union or whatnot.
Andy Ross