Hello,
I have a very strange problem. I have created a code that I beleive is multicore/parallell threading to make a task to run faster using more cores in the computer.
My computer is a server:
32 cores (2 CPU with 16 cores each)
32 GB
Refering to my code:
- If I use 1 process (1 core), the task takes 12 minutes.
- If I use 2 process (2 cores), the task takes 7 minutes (This is what my goal is with multithreading)
- If I use 4 process (4 cores), the task takes also 7 minutes (How is this possible? It should take 3.5 minutes?)
- If I use 25 process (25 cores), the task takes 18 minutes. (But what happens with 25 cores. It takes even longer time?)
As seen if I use 25 processes, it even takes longer which is my very big question mark here. I have no idéa why this is happening.
The thing is that I have 50 files that I will read, so every file if using 1 core takes: 12/50 minutes = 14 seconds
Now I do start 25 backgroundworkers and send along 2 files to each backgroundworker in hope that this will go much faster, in theory approx: 14 seconds x 2 = 28 seconds to complete all 25 backgroundworkers.
All backgroundworkers do work and in the end they produce the same result as 1 backgroundworker doing all 50 files. But it takes 18 minutes.
I simply can't understand why this is happening and what I am doing wrong. There is no code that blocks like lockobjects in the backgroundworker where only one worker can work at a time, so there are nothing that should stop it from running freely.
My code example is like below:
//Form1.cs
using System;
using System.Collections.Generic;
using System.IO;
namespace HelloWorld
{
private void button1_Click(object sender, EventArgs e)
{
//Use 1 core out of 32 cores (This process will take 12 minutes)
int numberOfProcesses = 1;
//Use 25 cores out of 32 cores (This process will take 18 minutes which is VERY strange? This should go faster than 12 minutes?)
//When I use 25 cores. I divide the work in 25 pieces, so each process only needs to do 2 tasks instead of 50 tasks when only using 1 core.
numberOfProcesses = 25;
for (int i = 0; i < numberOfProcesses; i++)
{
HelloWorld.Instance ts = new HelloWorld.Instance();
//1 core sends along 50 files. 25 cores sends along 2 different files each loop
//I have not send along anything here for the purpose of the example and structure of code
ts.doSomething();
}
}
}
//Instance.cs
using System;
using System.Collections.Generic;
using System.IO;
namespace HelloWorld
{
public class Instance
{
public void doSomething()
{
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.WorkerSupportsCancellation = true;
bgWorker.WorkerReportsProgress = true;
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.ProgressChanged += (sender, args) => { progressCallBack(args.UserState.ToString()); };
bgWorker.RunWorkerCompleted += (sender, args) => { completionCallBack(args.Result.ToString()); };
//Start backgroundWorker
if (bgWorker.IsBusy == false)
{
bgWorker.RunWorkerAsync();
}
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
String getline = ""; StreamReader reader = null; FileStream fs = null;
fs = new FileStream("C:/file1.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); reader = new StreamReader(fs);
while (reader.Peek() >= 0)
{
getline = reader.ReadLine();
//Do ALOT of work here
//This takes 12 minutes with 1 Core
}
reader.Close(); fs.Close();
}
}
}