views:

60

answers:

4

Hi folks,

I've been implementing this little game idea, which is (somehow?) similar to Conway's Game of Life:

0) You have a matrix of colored dots (RGB values) 1) If the adjacent cell has a lower X value than your Y, put Y = 0 on that cell (Where X and Y are Red || Green || Blue) 2) Red beats Green beats Blue beats Red

What I'm doing now it's just going cell by cell, checking if the above rules are met. However, the behavior it's not quite what I intended since sometimes cells on the first rows have advantage over those at the ending rows.

Can multithreading prevent this (say, launching two threads, one initiating in the first cell and the other on the last one)? Please pardon my ignorance on concurrency, but I felt this was a nice way to begin working with it.

+1  A: 

You'll be better off adapting your algorithm to prevent this.

Relying on multithreading to change behavior is not a good thing. This is, in essence, trying to introduce a race condition into your code. Normally, when adding multi-threading to an algorithm, the first priority is to prevent any changes in behavior.

By trying to use a race condition to change behavior, you're making this very non-deterministic, but not in a good way. You'd be much better off trying to come up with a different solution to this (potentially using a pseudo-random number generator, etc), and then introducing multi-threading to make it faster (hopefully without affecting the results).

Reed Copsey
+4  A: 

My guess is that you are updating the matrix inplace, whereas you should copy keep a track of the old state of the matrix, updating a new one, then replacing the original one by the updated. This way, you won't update some cells, then on the next line test their values. Thus, it would be an algorithm problem, not related with programmation (and therefore multithreading cannot help).

Scharron
But once you have changed to use a second matrix, this would be something that could be sped up significantly by multi-threading. If you created two threads, one doing the top half and the other doing the bottom half (so that they never wrote to the same memory space) you wouldn't need any synchronization at all--they would just both fly at full speed.
Bill K
@Scharron: Thanks for the advice. Indeed, I was updating the matrix ipso facto rather than storing changes and apply them at the end. I'll change it using DeadMG idea.@Bill K: You mean using multithreading to apply the changes from the new matrix into the original matrix?
Gastón
@Bill K: Sure, but the question was about multithreading and first imbalance of of rows.@Gastón: You can imagine several ways to multithread your program. But in this case, you would have one thread to print the current state of your game, and then if your matrix is big, you can have several threads to update disjoints part of the matrix. For example with 2 threads, you can update halves of the new matrix, reading concurrently the same original matrix, then copying to the original matrix when both threads finished their work.
Scharron
Quick optimization: Keep pointers for the 'current' and 'working' matricies, and when you've computed the next generation, swap the pointers. No copying involved!
Karmastan
+2  A: 

No. Your problem is an inherent flaw. The trouble that you have is that you're using intermediate results, i.e., the change in one cell affects the next cell immediately, in this update. It shouldn't. You should create a new matrix, store the changed values in there, and then swap them so that the new values are loaded. Repeat.

DeadMG
Yes, you're right. At first I thought about having a queue storing the modifications, and once all cells were processed, start dequeuing and applying changes. I eventually discarded it (bad move :) ), but the ideas are similar (though yours seems simpler).
Gastón
A: 

It depends on what part of the processing you choose to multithread. The prototypical multithreading example is the matrix multiplier. You can basically break it up into quadrants and calculate one quadrant in each thread, with no sharing of information except the original matrix. Note that the Game of Life is a sparse matrix, though, and may or may not benefit from multithreading.

However, if you do decide to do so, keep in mind that everything should calculate what it needs to for the "next turn" and place it in a new matrix, when swap-in the matrix (preferrably not copy, just change a pointer someplace) at the end of the turn, so that one thread isn't changing the values that the other ones need to do their calculations. So the thread's can't be allowed to "get a turn ahead" of each other. This might mean that it turns out to be inefficient to do with multiple threads - your mileage may vary.

eruciform