Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 184 185 [186] 187 188 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887806 times)

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2775 on: August 16, 2012, 07:48:58 am »

Hmm. So is using multiple processors harder than making use of multithreading?
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2776 on: August 16, 2012, 07:59:28 am »

Found a Fun article for all you PHP fanatics.
I currently work with PHP, and have to agree with everything he said. It's an abomination if one is more used to strict languages. Currently I'm using a framework (Yii) that addresses a lot of these complaints, though.

Spoiler: Obligatory Java Bash (click to show/hide)
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2777 on: August 16, 2012, 11:01:35 am »

Hmm. So is using multiple processors harder than making use of multithreading?

No, not really.

That article is talking about a multithreading cpu, which is lightly different than a multi-threaded programing.

A multi-threaded program would run happily on either a single multi-threaded processor or on a multi processor system.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2778 on: August 16, 2012, 12:43:08 pm »

With multithreading you are effectively saying "see this function? run it separately." The OS takes it, throws it in with the other things requiring run time (like background processes and such), and it is then managed automatically and sent to wherever is available. And in fact, your single-core program will have this happen to it too if the OS decides to move it to a different CPU core. Watching Ubuntu's task-manager-equivalent program shows my code swapping to a different core every 5 seconds or so; though this is OS dependent behavior and is pretty much entirely managed by the OS (though you can change how high your code's priority level to get run time is within your code in some cases).

The trick with multithreading is to ensure things aren't being accessed by multiple things at the same time; and so there are special atomic commands to help prevent it. Say for example you have a variable 'int x=0;' and this variable is accessed by both thread A and thread B. Thread A needs to run 'x = x + 2;' while Thread B needs to run 'x = x - 1;' Even if the order in which they are run doesn't matter, there comes an important problem which can result in funny things. If run in normal sequential order, x should be equal to 1 at the end. If run with multithreading, you can't know what it will be without proper locks.
'x = x + 2' would compile down to assembly saying something like:
'tmp1 = loadWord(xAddress); tmp1 = tmp1 + 2; storeWord(xAddress, tmp1)'
'x = x - 1' would similarly compile down to something like:
'tmp2 = loadWord(xAddress); tmp2 = tmp2 - 1; storeWord(xAddress, tmp2)'
Now, this means this single line of code needs 4 instructions to run. So, if Thread A and Thread B both run, you may have something like this:
'x = 0;'  <--- value set previously in RAM/cache or where ever it's stored
'tmp1 = loadWord(xAddress);' <--- value loaded into register for Thread A
'tmp2 = loadWord(xAddress);' <--- value loaded into register for Thread B
'tmp1 = tmp1 + 2;' <--- add 2 to tmp1 for Thread A; tmp1 now equals 2
'storeWord(xAddress, tmp1);' <--- tmp1 stored back at RAM/cache/wherever location by Thread A; Thread A finished with its code now
'tmp2 = tmp2 - 1;' <--- subtract 1 from tmp2 for Thread B; tmp2 now equals -1
'storeWord(xAddress, tmp2);' <--- tmp2 stored back at RAM/cache/wherever location by Thread B; Thread B finished with its code now
So, if we were to check the value stored in 'x' at this point, it would be -1. Similarly, if Thread A took longer to complete than Thread B, it would be 2. Most of the times code executes on Thread A and Thread B, one is run after the other, as 3 instructions is a tiny amount of time. However, if you were to run them both 100 times, you would quite likely not get 100 as the value for x. These sorts of things are called race conditions.

So, we need a way of telling Thread A and Thread B "Don't execute this specific part of your code if the other thread is executing that specific part of it's code." So, you could try a boolean flag shared between the two.
if(flag)
wait
else
{
flag = true
execute code
flag = false
}
However, this runs into the same problem as above; there is a chance one could see the flag is false, branch into the else statement, and before it has a chance to change the flag, the other thread enters its else statement. The flag is then set to true, but both are already inside the else statement.

The special multithreading things are atomic (that is, indivisible, single instruction units) operations. They combine the 'if' functionality with 'set' functionality; 'check and set' operations. These go by the names of semaphores and mutexes, among others.

While these deal with the fundamental issues of multithreading, the tricky part is using them. It requires careful consideration of interdependencies between code run as separate threads to prevent race conditions which will generally slowly corrupt the values of any variables involved.
« Last Edit: August 16, 2012, 12:50:06 pm by alway »
Logged

Derekristow

  • Bay Watcher
    • View Profile
    • Steam ID
Re: if self.isCoder(): post() #Programming Thread
« Reply #2779 on: August 16, 2012, 05:51:56 pm »

I wrote out this post thinking I'd be contributing, but I basically restated what Skyrunner said.  Ah well, might as well post it anyways :P.

Code: [Select]
while (user has not quit)
    clear the display
    process and render the title screen to the display
    if (user has pressed a key)
        open a menu
(Each submenu is also rendered and calculated using a basically the same process.)

This sounds like your problem.  There is usually one while loop running basically the entire program, calling a sort of tree of update functions, then a tree of draw functions each frame.  You end up having to store more information about the state of an object, but as far as I know it's the standard for programs that need to update continuously.  Keeping update and draw separate also lets you do fancy stuff like kicking drawing into its own thread, dropping graphical frames when things get too intense for your graphics card to handle, etc.
Logged
So my crundles are staying intact unless they're newly spawned... until they are exposed to anything that isn't at room temperature.  This mostly seems to mean blood, specifically, their own.  Then they go poof very quickly.

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2780 on: August 17, 2012, 01:42:00 pm »

I'm working on a procedural residential architecture generating program. I hope that in time, it would eventually be able to consistently produce believable architecture with enough detail to be usable.

Right now, it is basically capable of describing a 1 room shed constructed from random crap, though I can make it more realistic by limiting the number of material layers, types of materials and add rules for the ordering of layers.

The next phase is making it put together trailers, dividing them is also easy. they will always have a living room crossing the width of the trailer, and a kitchen/dining room crossing the width of the trailer with 1 to 4 bedrooms at one or both ends with an optional laundry closet.

things get more difficult when you try to arrange a house that isn't squeezed into an elongated rectangle. Even fulling out  believable rooms in a square or a rectangle large enough that main rooms won't always fill the full width is difficult. Much less the extremely common L and T, or more complexly shaped houses.

I was only able to find 1 internet source for anything like this, and it was an incomplete work to say the least. The one thing mentioned, but not demonstrated was creating the house as a bidirectional graph of rooms with assigned magnitudes, and then growing those rooms like rectangular bubbles connected by springs that expand and deform to create a tightly packed but not entirely regular floorplan. But I have no idea how I would actually program something like that.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2781 on: August 17, 2012, 01:44:38 pm »

Nice!  Does it just do layouts, or does/will it do structure architecture as well, like load-bearing walls and such?  Frankly either is pretty awesome.
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2782 on: August 17, 2012, 02:00:02 pm »

Nice!  Does it just do layouts, or does/will it do structure architecture as well, like load-bearing walls and such?  Frankly either is pretty awesome.

It fakes structure.

Starting with with a foundation it generates a set of layers from a list Then it puts in a vertical structural material like steel beams, oak 2x4s or concrete blocks and then adds optional external and internal layers with some logic for necessity of layers/materials of materials (if you have a 2x4 verticle structure, you need some kind of external layer but don't necessarily if you have a concrete block structure).

, at the moment, you occasionally get concrete slabs on top of carpet on top of steel beams with the whole thing wrapped in a blue tarp. But you never know with those crazy rednecks. When I get to the point where I am building actual houses the layering rules and material variety for the various purposes will have to be more strict... unless I am content with building junk towns.

I am still not sure if i will be able to make the layout as a bubble simulation thing work at all though.
« Last Edit: August 17, 2012, 02:02:14 pm by Nadaka »
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2783 on: August 17, 2012, 03:31:54 pm »

That is still pretty awesome as a start.  The fact that its attempting actual structural architecture at all is pretty amazing.


*dreams of one day seeing AI's designing skyscrapers*
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2784 on: August 17, 2012, 08:59:17 pm »

Spoiler: Obligatory Java Bash (click to show/hide)
EPIC BURN
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2785 on: August 17, 2012, 09:08:43 pm »

That is still pretty awesome as a start.  The fact that its attempting actual structural architecture at all is pretty amazing.


*dreams of one day seeing AI's designing skyscrapers*

If you only dream of looking at skyscrapers from the outside, that is a more or less solved thing. There are multiple methods of creating pretty skyscrapes using a few simple volumetric extrusion techniques and applying a really good set of bitmapped window/concrete/steel.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2786 on: August 18, 2012, 02:36:42 am »

Spoiler: Obligatory Java Bash (click to show/hide)
EPIC BURN
Spoiler: Even More Java Hatred (click to show/hide)
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2787 on: August 18, 2012, 03:12:48 am »

...Thankfully, I believe most of the integrated systems stuff is written in either assembly or C. :P

Today, I was coding a simple program that would simulate wave motion, and can be read using a number of functions like at(int x, int y) to simplify using 2d vectors. And of course, from somewhere an out-of-bounds exception occurs D: I'm too lazy to go hunt for it right now... *procrastinates*

I bet it's the character array I use to change output from values to characters that are printed D:

Is there an easy way to output a value to a character string of string type? Or vector type.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Elu

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2788 on: August 18, 2012, 03:21:22 am »

Spoiler: quote from Nadaka (click to show/hide)
whoa... what language are you using?

Spoiler: my tinkering (click to show/hide)

edit:
...Thankfully, I believe most of the integrated systems stuff is written in either assembly or C. :P

Today, I was coding a simple program that would simulate wave motion, and can be read using a number of functions like at(int x, int y) to simplify using 2d vectors. And of course, from somewhere an out-of-bounds exception occurs D: I'm too lazy to go hunt for it right now... *procrastinates*

I bet it's the character array I use to change output from values to characters that are printed D:

Is there an easy way to output a value to a character string of string type? Or vector type.
hmm a cast? also what languare are you using? IIRC in c there is the "itoa" function that should do the job.
« Last Edit: August 18, 2012, 03:28:36 am by Elu »
Logged
Dwarf Fortress, a game which learning curve is a Himalayan trail covered in blood. and alcohol. on fire.

English is not my mother tongue, please point out my mistakes and help me improve it : )

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2789 on: August 18, 2012, 03:48:49 am »

No, if you cast it it becomes wacky.

Code: [Select]
int (-237) -> SUDDEN WEIRD STUFF if you cast it to a char.
int (48) -> char (0)

And itoa() seems to have the same problem as sprinf() does: it needs an array of characters, not a string or vector. And arrays of characters always seem to invite out of bounds errors D:

I might be doing it wrong, though. Perhaps I need an iterator where it wants a char * str argument?
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward
Pages: 1 ... 184 185 [186] 187 188 ... 796