Announcement

Collapse
No announcement yet.

Thread Set Priority

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Thread Set Priority

    In apps like gbBuilder, I sit around and wait a lot when creating index files. The functions involved are reading/parsing/writing thread files.

    I've noticed these actions vary greatly in their time of execution.

    So, I wondered if Thread Set Priority can influence the speed with which the background work is done? Dedicate more/full PC resources

    I have zero experience with thread priorities, so if someone could talk about the pros and cons of using thread priorities, then I'm all ears!

  • #2
    It has not escaped me that when dealing with 50K files sequentially, that dividing the work into multiple threads might have a big impact on execution time. I intend to look at that in more detail as well. I don't want to overly complicate the code, but taking a 40min task down to 5min is work some additional complication.

    Not having looked at creating multiple threads to process 50K files, I many find it easier than I think.

    But for this thread, I'd like to get a better handle on whether Thread Priorities can play a role in improving execution.

    Comment


    • #3
      Originally posted by Gary Beene View Post
      But for this thread, I'd like to get a better handle on whether Thread Priorities can play a role in improving execution.
      https://blog.codinghorror.com/thread...ties-are-evil/

      Comment


      • #4
        Howdy, Stuart!

        Thanks for the link.

        The discussion there certainly would tend to dissuade someone from using thread priorities, in part because the results seem a bit unpredictable.

        But, I find it hard to believe there are no circumstances in which thread priorities can be relied upon to achieve better thread performance. The discussion was mostly "don't do it" as opposed to "if you do it, make sure you do it this way". At least, that's what I thought it said.

        I'll want to nose around some more before discarding the possibilit that thread priorities might be helpful.


        Comment


        • #5
          Are you using two SSD drives with virus checker off?
          I hope you looked into using a file sort/merge instead of a record sort.
          How long is an idea? Write it down.

          Comment


          • #6
            Gary, you might play around with system file cache such as
            https://docs.microsoft.com/en-us/sys...loads/cacheset
            http://www.analogx.com/contents/down...b/Freeware.htm

            and/or having a large ram drive

            p purvis

            Comment


            • #7
              Howdy, Mike!
              Thanks for the questions. I use just a single SSD. Virus checker is on.

              I don't do much sorting. What I have uses Array Sort and takes about 2-3 minutes to sort the 15 million word array. It's not a factor in my wait time.


              And, hi Paul!
              Thanks! I'll take a look at the links.

              Comment


              • #8
                Gary,
                it's unlikely that setting thread priority would have any discernible effect on the speed of your code.
                Multiple threads is a different matter and can increase the speed considerably. That's the way to go but will only work if you're being limited by processing power and not by disk bandwidth.
                Look at task manager and see what's limiting throughput.

                Comment


                • #9
                  "so if someone could talk about the pros and cons of using thread priorities,"

                  Thread priority is probably not what you're looking for. It has very specialised uses.

                  The OS schedules threads according to the process priority first then the thread priority second.

                  All threads with a specific overall priority are treated equally and scheduled a timeslice, if they require it.
                  Only if no thread of a higher priority needs a timeslice will a thread of lower priority be considered.

                  If a thread of higher priority stops being idle and requests a time slice then the timeslice will be immediatley allocated even if that requires the current lower priority thread to be immediately suspended to give up its timeslice.

                  These days, most CPUs have multiple cores so they can process multiple threads at the same time. If a higher priority process requests attention, it'll be given an idle core immediately without needing to suspend your lower priority thread on the other core.


                  If your program is the only CPU intensive program being run then changing the thread priority will have no effect. There's no other significant process competing for CPU time so your code will get all the cpu time it can handle, regardless of its priority.


                  Changing thread priorities might help in some very limited circumstances but it's rare. e.g. Some hardware may require immediate servicing or data might be lost so the thread running that hardware will need a high priority.
                  Making your code a higher priority doesn't gain you significant extra processing time, it just disrupts the very small number of processes which really require the higher priority to function.


                  Thread Priorty is not going to make your program run any faster.
                  But threading your program so it can process the data in multiple threads, each running on a different CPU core, can make your program a lot faster.

                  Comment


                  • #10
                    "But threading your program so it can process the data in multiple threads, each running on a different CPU core, can make your program a lot faster."

                    I am so looking forward to seeing how this evolves! I have wanted to learn about dividing up a complex task to be handled in multiple threads, and so I'll be lurking on this for sure! So many questions - like, how to know what parts to thread, and in what ways? Is there some guideline about how to divide the task, and how to manage results, like do they need to be re-assembled?

                    -John

                    Comment


                    • #11
                      "But threading your program so it can process the data in multiple threads, each running on a different CPU core, can make your program a lot faster."

                      If that exercise is done many times thread pooling is more efficient. From Microsoft: "An application that creates and destroys a large number of threads that each run for a short time. Using the thread pool can reduce the complexity of thread management and the overhead involved in thread creation and destruction." With my Encrypternet application I could be decrypting a 1GB file streamed in via a 256KB buffer. The buffer is decrypted and hashed using two threads of execution and that job is passed 4096 times, 1GB/256KB. Thread creation, on my machine, costs about four times as much as a thread submission with a Thread Pool so the latter was employed. The performance boost was staggering. Since the SHA256 took longer than the decryption the latter was effectively done free of charge.

                      I also used thread polling with CryptoRndII. The throughput with thread creation/destruction was 35MHz, with thread pooling the throughput increased to 338MHz.

                      Comment


                      • #12
                        John,
                        not everything can be sped up using threads. It depends on the task.
                        In this case, thousands of files are being scanned and indexed.

                        If the majority of time is taken up processing the files, rather than reading them from disk, then It would make sense, if you have a 4 core CPU, that the files are split into 4 groups, each group being indexed by 1 core of the CPU and then the 4 indices are merged at the end.

                        There are examples of using threads to split tasks:
                        https://forum.powerbasic.com/forum/u...515#post763515


                        Comment


                        • #13
                          John

                          I have not written using threads for decades!

                          But surely the operational rule is ....

                          List all the processes that can be done now
                          A
                          B
                          C
                          etc
                          List all the processes which can be done when A is completed and then B and then C and then A&B and then A&C and then B&C Then A&B&C

                          and so on.

                          Then call your threads appropriately.

                          A bit like a PERT chart (although you can go backwards too)
                          [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
                          Kerry Farmer

                          Comment


                          • #14
                            I've used multiple threads and can attest to their ability to achieve much greater speeds ...

                            Multiple threads is a different matter and can increase the speed considerably.
                            I recently saw a Large Text File Editor which had the option to set priorities. That raised the question for me whether setting priorities could enhance performance in apps such as gbThreads.

                            It would seem that the answer is no.

                            Comment


                            • #15
                              Gary,
                              Have you tried partitioning another drive and turning off virus checker?
                              Reading and writing using contiguous free disk space should greatly increase performance.
                              Since string concatenation is extremely slow is there any code to look at?
                              Can you monitor if the read/write operation slows down after thousands of files.
                              How long is an idea? Write it down.

                              Comment


                              • #16
                                Also Gary. If your not using the option LEN with the OPEN statement. You might want to give it a go. The results would be interesting.
                                p purvis

                                Comment


                                • #17
                                  Hi Gary, you likely have a ssd drive in you computer.
                                  You might want to run sdelete from sysinternals
                                  sdelete will take some time to run, go get a pepsi and walk slowly up the stairs.
                                  If you have windows 10, it is likely defragging the ssd once a month. More on that later somewhere else.
                                  for the c: drive the command line would be sdelete c: -c
                                  If your software has some kind of retriming then execute it.
                                  If you drive is a samsung drive and if you have samsung magican the you might consider turning on Rapid Mode.
                                  Rapid Mode takes memory and caches something to do with the samsung ssd drives.
                                  After your done with your extreme file processing, you might want to disable Rapid Mode.
                                  p purvis

                                  Comment


                                  • #18
                                    You may know this... Threads can actually make the program slower due to context switching. My first TCP IP server used 1 thread per connection. I am upgrading it now to use 1 thread and overlapped asynchronous methods. The program still has a few threads; GUI, Communications, Watch-Dog and Database.

                                    Are you actually running simulations parallel processes? One way I do this stuff sometimes is I take the functionality of one of the threads and put it in its own program. Then I work on getting the most performance for that program as a stand alone. Then I only merge them back into one program when I am sure I have the best possible "mini-program" for the thread.

                                    Comment


                                    • #19
                                      you may know this... Threads can actually make the program slower due to context switching.
                                      Kind of ... you have to be careful to write your thread functions so they do not require any thread switching.

                                      For example, if you are running a "background" task but in that thread function ask for the contents of a control executing in the GUI thread.. that forces a thread switch.

                                      So yes... using additional threads can slow down what you are doing if you are adding thread switches to the job.

                                      MCM




                                      Michael Mattias
                                      Tal Systems Inc. (retired)
                                      Racine WI USA
                                      [email protected]
                                      http://www.talsystems.com

                                      Comment

                                      Working...
                                      X