-2

I use dual-core, delphi xe6 and api which doesn't support multi-core. The application coded with these makes full load of one core. Can I disperse the load to the other core?

JO SeongGng
  • 567
  • 3
  • 18
  • 3
    Split the work into separate pieces that can be executed in parallel, and then execute the work in parallel. There's no magic switch though, you have to learn how to do this, understand and decompose your program, and then do the work. – David Heffernan Jun 25 '15 at 11:02
  • This question is either far too broad or far too simple. The direct answer to your question is **"maybe"**. Some algorithms and workloads are parallelizable, others are not. As @DavidHeffernan said, there is no simple toggle switch and there is no suitably general method that can blindly be applied to effect this. In some situations it can be incredibly easy - in others it can be incredibly difficult. It all depends on your specific problem and the code you have to hand - we don't know anything about either of these and therefore cannot offer any relevant advice. – J... Jun 25 '15 at 16:57

2 Answers2

1

If you want to utilize all available CPUs and increase performance of the app, then you have to rewrite your app as it is suggested in another answer. IT WILL IMPROVE PERFORMANCE.

If you just want to spread executing of the app over all available CPUs, for example to get 25% load for every CPU of quad-core processor (instead of 100% load of single CPU), then it should be enough to set correct affinity mask for process, such task already discussed here for example. But it also depends on OS settings, Windows may limit number of CPUs available for app. IT WILL NOT IMPROVE PERFORMANCE.

Demo project for David:

procedure TForm18.FormCreate(Sender: TObject);
begin
  while not Application.Terminated do
    Application.ProcessMessages;
end;

Affinity mask 1 (4 CPUs allowed): enter image description here Affinity mask 2 (1 CPU allowed): enter image description here

Definition from MSDN: A process affinity mask is a bit vector in which each bit represents the processors that a process is allowed to run on.

Community
  • 1
  • 1
Andrei Galatyn
  • 3,322
  • 2
  • 24
  • 38
  • 2
    A single-threaded application will not run faster on a multicore (multiprocessor) or on a multithreaded single processor without changes. – mg30rg Jun 25 '15 at 14:04
  • @mg30rg Obviously. But in the question i see verb "disperse", it could be just question about how to spread/disperse load from one CPU to all CPUs. Performance improvement is not mentioned directly in the question and i didn't mentioned it in the second part of my answer. – Andrei Galatyn Jun 25 '15 at 19:35
  • 1
    I can't make much of this. It's not a given that multithreading will improve performance. It can make it worse. It's also invariably a bad idea to set affinities. I don't know why you would suggest that. – David Heffernan Jun 26 '15 at 06:40
  • @David 1. Performance - is the only reason why we use multithreading, of course it is not simple, of course you should understand how and where use it, ... 2. If he really has 100% load of single CPU and needs to spread load over both available CPUs (to get 50% + 50% load instead of 100% + 0%), then i guess affinity mask is the only way, isn't it? – Andrei Galatyn Jun 26 '15 at 07:20
  • 1
    No. You've got that badly wrong. If there are two busy threads then the scheduler will run them on different processors. Recommending affinity setting is very bad advice. – David Heffernan Jun 26 '15 at 07:33
  • @David But scheduler may run anything only on allowed CPUs. If affinity mask restricts process to use CPU-1 only, then only CPU-1 will be loaded (even multithreading doesn't help to utilize other CPUs in this case). Let's forget for a second the original question, it is not clear anyway. If my 1-thread application uses 100% of 1 from 4 CPUs, but i want to load 4 CPUs (25% of each) instead. I see only one case why it may happen and only one way to solve - check affinity mask and set it correctly (correctly from point of view of my needs). What is wrong here and what you suggest instead? – Andrei Galatyn Jun 26 '15 at 08:16
  • 1
    Your single thread program will only ever load 1 CPU at any one time. – David Heffernan Jun 26 '15 at 08:23
  • @David It is just playing with words. I don't care what happens at any specific moment in time. Performance is number of operations in [period of] time, "at any given time" means there is no period, in this case we can't speak about performance at all. If i open task manager, of course it shows statistics for some period of time, and most of the people are ok to use it as actual info (at any given time). With 1-thread application we will see there: 100% load of 1 from 4 CPUs if affinity mask lock process to single CPU, and 25% load of 4 from 4 CPUs if affinity mask allows it. – Andrei Galatyn Jun 26 '15 at 08:42
  • 1
    No. that won't happen. With a full mask, the scheduler will, assuming nothing else is happening, leave the single thread on the same processor. So far as I can tell, your answer says nothing more than "do what the other answer says". – David Heffernan Jun 26 '15 at 09:14
  • @David I added demo project and screenshot to the answer. – Andrei Galatyn Jun 26 '15 at 09:31
  • 1
    You are interpreting it wrong. If there's nothing else happening on the machine, then the scheduler will leave the busy thread on the same processor. Usually there's something else running on the machine and so the thread bounces around between processors from time to time. You've not said anything in this answer more than "do what the other answer says". And then given some confusing advice about affinity masks. – David Heffernan Jun 26 '15 at 09:39
  • @David "A process affinity mask is a bit vector in which each bit represents the processors that a process is allowed to run on." (c) MSDN. Practical experiment shows same thing, doesn't matter what happens in the system, process uses only CPUs allowed by affinity mask. Could you please provide any proof that it is me who understand it wrong? Any link to documentation or demo? – Andrei Galatyn Jun 26 '15 at 10:01
  • Never mind. If you are convinced that setting affinity masks will help the asker. And you are happy with the rest of your answer that says, "do what the other answer says", then that's fine. – David Heffernan Jun 26 '15 at 10:10
  • @David. If problem is performance, then everything is said before my answer. If problem is loading of 1 CPU instead of spreading over all CPUs, then affinity mask is where solution can be found. That is why i added my answer. And finally we just discussed what process affinity mask is and how it works, where i think you was wrong :) – Andrei Galatyn Jun 26 '15 at 10:14
  • You understand what the affinity mask does. It's just that there's no place for it in an answer to this question. – David Heffernan Jun 26 '15 at 10:16
  • @David If you understand the question, then you know more then me. I think even author doesn't understand own question. I see at least two interpretations, that is why i believe it is ok to leave here answers for both. – Andrei Galatyn Jun 26 '15 at 10:25
  • In my opinion it is a very poor question that should be closed and deleted. Answering doesn't help in my view. – David Heffernan Jun 26 '15 at 10:27
  • @David I agree with you in first part, it would be better to delete the question. – Andrei Galatyn Jun 26 '15 at 10:30
  • @DavidHeffernan - I think the problem is more with the English knowledge of _JO SeongGng_ than with the actual question. As far as I see he (she?) was asking about how to perform tasks parallely in general, If - by any reason - this question has to be deleted, the reason would be **"too broad"** and not **"does not make sense"**. (Or maybe it is just easier to be empathic with non-native English speakers for me, because English is not my first language either.) – mg30rg Jun 29 '15 at 07:31
0

What you are looking for is more than likely multithreading. (Or maybe multiprocessing.)


You should rewrite your application to process data and perform tasks in a parallel manner. While that is not a really difficult task, the wording of your question suggests, that you are not familiar with the concept of parallel programing.

If you wish to learn how to utilize threads this SO question, this embarcadero article, and this delphigeek article might help you to find the general direction.

Community
  • 1
  • 1
mg30rg
  • 1,311
  • 13
  • 24