Results 1 to 21 of 21

Thread: [RESOLVED] Maximize cpu usage?

  1. #1

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Resolved [RESOLVED] Maximize cpu usage?

    Hi Guys!

    I am writing and compiling a windows form application with VS2012. I think it compiles the .exe only for single core usage. How can I make it to use all available cores in a computer??

    Thanks a lot!

    Edit: BTW, I wore the same application in C++, too... If there is a way to do this in C, I am also in
    Last edited by Flashbond; Feb 26th, 2013 at 08:28 AM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [RESOLVED] Maximize cpu usage?

    Why is this marked Resolved? Did you get an answer?

    VS doesn't compile a program for one core or multiple cores. Whether it makes use of multiple cores depends on whether or not you wrote it to be multithreaded. If you did, then it will be multithreaded whether there is one core or eight. What that would mean would be that it would schedule threads onto whatever core is available depending on system capacity and utilization. Of course, if you don't write it multithreaded, then it will use only one core no matter what you give it.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    Yeah, I figured the same thing out with reading a few more articles from Google that, is muchly depending how I did write the code. I saw a few example in msdn, too... But they are a bit tricky maneuvers for my level. Then, I changed my mind and marked the post as resolved.

    Infact I have two very simple loops. If I send you the code, can you modify it for me?

    Thanks for reply!

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Maximize cpu usage?

    If they are simple just post them here using code tags
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Me.TextBox1.Text = "" Then : Beep() : Exit Sub
            Else
                Dim l, n As Long : Dim m As Long = 1 : Dim p As New List(Of Long)(New Long() {2})
                For n = 1 To (Me.TextBox1.Text)
                    m = m + 1
                    For l = 0 To n
                        If m Mod p(l) = 0 Then : m = m + 1
                        End If
                    Next
                    p.Add(m)
                Next
                Me.TextBox2.Text = p(n - 1)
            End If
        End Sub
    Thanks a lot from very now!!!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [RESOLVED] Maximize cpu usage?

    Does that code even run? It certainly doesn't look like it should. It looks like the inner loop should throw an Index Out of Range exception since you reference p(l), but it doesn't seem like p has to be have l elements in in (unless that's a 1 rather than an L, since I can barely see a difference between those two).

    If the code doesn't run, what is it supposed to do? At first I thought it was a prime number hunter, but it isn't.
    My usual boring signature: Nothing

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: [RESOLVED] Maximize cpu usage?

    More to the point. There's nothing there to even suggest that multithreading is required or even possible!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [RESOLVED] Maximize cpu usage?

    That's kind of true, but it may be possible to thread a portion of it. At first glance, it looks like it could almost use parallelism, except that the behavior of loop N+1 is dependent on the behavior of loop N, so they look like the order of execution is important. Parallelism would destroy the order of execution. However, if there are sufficient iterations of the loop (you are using Long, so there could be MANY iterations), then it might be possible to divide the whole thing up into a series of ranges and run each range on a different thread, then merge the results back together at the end. This would not be all that straightforward to understand. but might not be terribly difficult to code.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    Yes, it is a prime number solver but follows a different squence to catch primes. So it has different results rather than the actual prime number order that I need one of my projects. For example, if you input 10, it'll show 27. It has also multipliers of 3 which is something comes from PI. And the right code is:
    Code:
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
           If Me.TextBox1.Text = "" Then : Beep() : Exit Sub
            Else
                Dim l, n As Long : Dim m As Long = 1 : Dim p As New List(Of Long)(New Long() {2})
                For n = 1 To (Me.TextBox1.Text)
                    m = m + 1
                    For l = 0 To n - 1
                        If m Mod p(l) = 0 Then : m = m + 1
                        End If
                    Next
                    p.Add(m)
                Next
                Me.TextBox2.Text = p(n - 1)
    
            End If
            End Sub
    So there should be a way to make it use all proccessors when it tries for, for example, more than millions?!
    Last edited by Flashbond; Feb 26th, 2013 at 04:39 PM.

  10. #10

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    Ok Shaggy, how to divide into iterations? If you don't want to modify this code, just show me a simple example please?

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,375

    Re: [RESOLVED] Maximize cpu usage?

    Just my dos centavos:
    Code:
    For n = 1 To (Me.TextBox1.Text)
    Turn option strict on and properly convert that string to an integer.
    Code:
    m = m + 1
    You could do m += 1 and get the same result, same thing can be done in C too.
    Code:
    Me.TextBox2.Text = p(n - 1)
    No real need to add Me. and .Text is a string, convert p(n-1) to a string using CStr or .ToString.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [RESOLVED] Maximize cpu usage?

    Yeah, it would be possible, but not so easy.

    The first thing you should do is turn on Option Strict for the project and fix all the errors that will result. In a case like this one, that may well give you a noticeable performance boost. It may not, as that is a bit hard to say without testing, but it is a good habit to get into anyways. You might also try turning off Integer Overflow Checks, since you are doing lots of integer math, so that may help this out a bit.

    However, I assume that the big number is in Me.TextBox1.Text. The only way to thread this effectively would be to divide that number up into chunks and process each chunk in a different thread. Threading the actual work in the loop will gain you absolutely nothing, because you'd have to lock the whole thing due to its dependence on the value of m, which would force it back to a single thread anyways (actually, it would probably end up being worse than a single thread).

    Therefore, what you would have to do would be this: Suppose that the user entered a value of 1,000,000,000,000. You would have to divide that into a series of chunks, such as four chunks of 250 billion, or 1000 chunks of 1 billion, then move that method out of the button handler and change it so that it would work for any chunk. In other words, the method would have to take arguments for the starting and ending number and loop from the starting number up to the ending number. Then you'd figure out how many chunks, and create a new thread for each chunk (actually, you'd be better off using tasks, these days). Once all threads had completed, you'd have to be able to sew the results of each chunk back into a whole.

    There are a few problems with that design. The biggest problem is that you have no idea how many chunks to make because the only way to optimize the performance would be through trial and error. You aren't likely to get more threads running than you have cores in your processor, because the OS will spin up new threads based on available processor time, and these methods won't leave much of that. Therefore, you'd want the chunk size to be pretty large because it is likely that the number of chunks should be kept fairly small, perhaps even down to the number of cores you have. Even then you'd have a certain amount of a penalty for context switching, sewing the results back together, and the like, so even if you had four concurrent threads (which is somewhat unlikely on a quad core since the OS will be doing things, too), you wouldn't get a 4x speed boost.

    A better option might be to move the whole thing into something like a BackGroundWorker component and just let it run. The BGW will raise progress events on the UI thread when you want it to, so you could have it report new finds as they show up. The UI would remain responsive and would keep updating, while the BGW did the hard work in the background. You wouldn't get the boost from using multiple threads, but it would be considerably easier to code.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    OK, I checked the other forums and articles. Long story short, I couldn't manage it. Maybe I have to wait to be better. Thanks guys!
    Last edited by Flashbond; Feb 28th, 2013 at 08:08 AM.

  14. #14

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    After a long time of thinking, I came up with 2 ideas:
    1.)Maybe a logic like: If p array size will be more than 4, then;
    Check p(l) like
    Thread1:
    Check for p(l)
    Thread2:
    Check for p(l+1)
    Thread3:
    Check for p(l+2)
    Thread4:
    Check for p(l+3)
    again
    Thread1:
    Check for p(l+5)
    So in the same moment the divisibilty of m value will be checked for 4 p(l) values in the same time, parallel with...
    or
    2.)Divide p(l) into chunks?
    If p reaches to particular amount, divide into 4 chunks and put a boolean =false for each check. Then, if boolean still true for each chunk check then p.Add(m).
    Last edited by Flashbond; Mar 2nd, 2013 at 08:22 AM.

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [RESOLVED] Maximize cpu usage?

    I think option 2 is the only viable one. Can you really know the size of P() in advance sufficient to do option 1?
    My usual boring signature: Nothing

  16. #16

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    Sorry for my English. I didn't get the "Can you really know the size of P() in advance sufficient to do option 1?" question. Can you ask in a different way? Has it got something to do with counting an array?

  17. #17

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    Ok, Shaggy. I am very very poor with threading. If you have a model in your mind, please share. Pleeeeaseee...

  18. #18
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Maximize cpu usage?

    What is the problem you are trying to solve? You mentioned that this was 'it is a prime number solver but follows a different squence to catch primes'. Could you describe how it is different?

    I have written a Sieve of Eratosthenes and never really find a way to make it multithreaded in a useful way.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  19. #19

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    It is also including some of this sequence in order to follow some logarithmic behaviour. Ok, if you say noway, I accept. I was just thinking about alternatives.

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [RESOLVED] Maximize cpu usage?

    Yeah, after looking over that loop a bit more, I'm afraid that the answer is: It can't be done. If the check of numbers between 1000 and 2000 had nothing to do with the check of numbers between 0 and 1000, then there would be a way to do it, but it looks like the check of any number depends on checks made on smaller numbers. That means that every number would have to be checked in order, which preculdes any threading, since there would be no guarantee that the threads would return in any order unless you did some kind of synchronization control, and in that case the threading would be slower than what you have. Multithreading is only worthwhile if it provides a benefit, and in this case it looks like there is no alternative that would provide any benefit at all.
    My usual boring signature: Nothing

  21. #21

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Maximize cpu usage?

    OK, case dismissed! Thanks a lot guys for all effords!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width