1

I have implemented meta-heuristic solver and utilized .NET 4.0 Parallel.For and Parallel.Foreach. It works fines on my medium-end machine. But the search is too intensified and consumes too much resources especially the CPU time on that on lower-end machine.

So I think I have to put an option to tune down intensity of the search when needed. I would like to get CPU utilization down without much touch on an algorithm. It is fine if the search completes slower as long as it won't lock up the machine and allows the other work aside.

I'm considering to put Thread.Sleep on methods as all threads are 100% CPU bound (no I/O). Does that gonna decrease intensity of CPU usage I need? Is there any better solution?

ipoppo
  • 369
  • 2
  • 11
  • http://msdn.microsoft.com/en-us/library/system.threading.threadpriority.aspx – L.B Sep 12 '12 at 19:22
  • Is it spanning two many cores / using too many threads? You can specify some limits: http://stackoverflow.com/questions/2002864/parallel-foreach-spawning-way-too-many-threads – RQDQ Sep 12 '12 at 19:22
  • 1
    Why do you care that it consumes too much CPU? Usually, the CPU time wouldn't be used anyway and the OS is smart enough with scheduling, so that your application shouldn't interfere with other processes in the system. – svick Sep 12 '12 at 19:32
  • @svick I would like it to run as background apps. Currently it is trying to freeze the whole machine :( – ipoppo Sep 12 '12 at 19:39
  • It will interfere. If this CPU-intensive app runs at 'normal' process and thread priorities and has enough threads to occupy all the cores, the effect on other apps like Office, browsers, etc. will be human-noticeable on low-spec boxes. – Martin James Sep 12 '12 at 19:40
  • @dondonchi It's actually freezing your machine? Then you could have some other problem, like excessive swapping. How much memory does your application use? – svick Sep 12 '12 at 19:40
  • @svick Not really frozen but closed. I have not look into that one a lot I will check that later, thanks. – ipoppo Sep 12 '12 at 19:45
  • 1
    Why don't you just use `MaxDegreeOfParallelism` to spawn a limited number of threads and leave some cores free? – Tudor Sep 12 '12 at 20:20

2 Answers2

-1

If you use diffrent threads I suggest the following:

Thread.Priority = ThreadPriority.BelowNormal

So the thread get less priority.

Edit

Okay, this solution is only accetpable, if it's not used with ThreadPool..

Here I'm not sure, but whats about:

Process.ProcessorAffinity
  • Gets or sets the processors on which the threads in this process can be scheduled to run.

If we limit the cores, we got more space for other work, right?

CodeTherapist
  • 2,776
  • 14
  • 24
  • I don't think this is a good solution when you're using `Parallel.For(Each)`, because it uses thread pool and those threads could be used from other parts of the process. Changing the priority of the whole process (and not each individual thread) would be a better solution. – svick Sep 12 '12 at 19:30
  • @svick - yes, so they are of lower priority too. I can't see any problem with it. Lowering the priority of the whole process would be even easier - saves explicitly lowering the priority of the pool threads etc. – Martin James Sep 12 '12 at 19:35
  • @MartinJames But what if other part of the application sets the priority to normal (maybe because responsiveness there is important)? Then it would be confusing, because you wouldn't be sure what priority the current thread has if you don't explicitly set it. Because of that, I think setting the priority of a thread pool thread is never the right thing to do. – svick Sep 12 '12 at 19:39
  • Is there any way to detect if it was run on low resource machine? – ipoppo Sep 12 '12 at 19:41
  • The OP has given no indication that individual thread priorities are being changed to improve the overall performance of the app. If the are, then setting the process priority lower will still maintain the individual thread priority heirarchy in the app. Intuitively, (an yes, I haven't tried it with Parallel.For), I don't see any issue. Empirically, setting the priority of explicit thread-pool threads to a lower level, (eg. to improve GUI performance), works fine. Have you tried it and found any problems? – Martin James Sep 12 '12 at 19:49
  • You can get system information with WMI and than you can setup the right config. – CodeTherapist Sep 12 '12 at 19:49
  • Setting the thread priority of a thread pool thread is a *really bad idea*. See http://blogs.msdn.com/b/harip/archive/2010/10/11/threadpool-work-item-priority.aspx – Peter Ritchie Sep 12 '12 at 20:09
  • Peter - I read your link. Does it make sense to you? So what if a threadpool thread gets preempted because a normal-priority thread becomes ready - that's kinda the point of lowering priorities, surely? Can anyone explain why this a 'really bad idea' when the 'really bad idea' is the original requirement? As far as I can see, the argument in the link is 'if you reduce the priority of a thread, it will be preempted by a thread of higher priority that becomes ready' - not really news, is it? – Martin James Sep 13 '12 at 06:34
  • @Martin James: Reducing the priority of a thread-pool thread is a bad idea because they are supposed to be anonymous. If a task assigned to a thread lowers its priority, you don't know which one it is. Then you will get tasks randomly running at low priority. I don't see how this can be helpful. – Tudor Sep 13 '12 at 06:39
  • Another point - if lowering the process and/or thread priorities is not the answer to reducing the impact of a long-running, CPU-intenstive app on 'normal' non-CPU-intensive GUI apps (Office, Firefox, IDE, VLC player), what is? – Martin James Sep 13 '12 at 06:39
  • 1
    @Tudor - I don't know what mechanism to use in C# to efficiently lower the priority of all threadpool threads in an explicit manner. Presumably, this would have to be done via some event hook on starting a new threadpool thread, (any ideas, anyone?). Lowering the process priority would work - all the threadpool threads would then get a lower scheduling priority in the usual Windows way - they would get preempted by the normal priority threads in Office, browser, media player etc. when they become ready. Still not convinced by 'really bad idea'. – Martin James Sep 13 '12 at 06:50
  • @Martin James: Traditionally modern OSes (including Windows) have always favored interactive apps over CPU-bound apps. When the user focuses an interactive app the OS will give it a priority boost to ensure that it can preempt any CPU task. In fact even background tasks doing I/O will be automatically favored over CPU tasks when an I/O interrupt arrives. – Tudor Sep 13 '12 at 06:56
  • @Tudor - yes, I understand that the GUI foreground tasks will be given a higher priority and that threads made ready on I/O are boosted. This also applies to threads in the CPU-intensive app. It's fairly easy to investigate the effect of a CPU-intensive app on mainstream GUI apps - make some loopy threads, set them running and fire up Offce, Firefox etc. and use them. Then try setting the process priority of the heavy app to 'Below Normal' with the Task Manager - it does make a human-noticeable difference. – Martin James Sep 13 '12 at 07:02
  • If you look for disable the automatically boosting of the focused application you can use `Process.PriorityBoostEnabled` set to false. – CodeTherapist Sep 13 '12 at 07:16
-1

(sticks head above parapet, preparing to get shot down with many downvotes..:)

If you use diffrent threads I suggest the following:

Thread.Priority = ThreadPriority.BelowNormal

So the thread get less priority.

[Yes, this is a straight copy of C Sharper answer, but without the later edits about threadpool threads and affinity-bodging. If you think this is good idea - upvote C Sharper, not me].

Alternatively, reduce the priority of the whole process with the Task Manager or other means. This will reduce the 'real' scheduling priority of all the thread in the process, (including the 'really bad idea' threadpool threads), and improve the user experience for other apps that are not CPU-intensive but should respond quickly when they need to do something. The lower-priority heavy app will still soak up all the remaining CPU and, if you decide not to edit this month's sales figures or watch the latest illegally-downloaded copyrighted films, will perform as well as if run at normal priority.

Martin James
  • 24,453
  • 3
  • 36
  • 60