LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2007, 07:00 PM   #1
nebojsa.andjelkovic
LQ Newbie
 
Registered: Oct 2006
Posts: 6

Rep: Reputation: 0
multi threading slower than single threading on dual core. why?


I have one problem. My C application iz slower when I changed it to be a multi threaded application on dual core machine. Application is about image processing but that is not important. But it is important that this application isn't slower on single core machine.

Does anybody have any idea? What is going on? How can multi threaded application be slower than single threaded on dual core?

Last edited by nebojsa.andjelkovic; 01-27-2007 at 07:01 PM.
 
Old 01-27-2007, 07:43 PM   #2
docinthebox
LQ Newbie
 
Registered: Sep 2006
Location: Indianapolis, Indiana
Distribution: Sidux, Debian, Knoppix, Puppy
Posts: 15

Rep: Reputation: 0
Blocking among the threads maybe? Did you check CPU load on the two cores? Are they both loaded most of the time or is one of the cores idle?
 
Old 01-28-2007, 07:47 AM   #3
nebojsa.andjelkovic
LQ Newbie
 
Registered: Oct 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by docinthebox
Blocking among the threads maybe? Did you check CPU load on the two cores? Are they both loaded most of the time or is one of the cores idle?
No, blocking among the threads, no, that isn't reason. And both cores are working. Problem isn't so trivial. I surmise that problem is about scheduler or something similar.
Does anybody have any idea?
 
Old 01-28-2007, 07:57 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by nebojsa.andjelkovic
No, blocking among the threads, no, that isn't reason. And both cores are working. Problem isn't so trivial. I surmise that problem is about scheduler or something similar.
So you really think the multi-threading / multi-core thing just doesn't work ?

Perhaps is it time for you to tell what O/S, kernel, threading library you are using, and explain why do you think your application is not the problem's root cause.
 
Old 01-28-2007, 10:52 AM   #5
nebojsa.andjelkovic
LQ Newbie
 
Registered: Oct 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlliagre
So you really think the multi-threading / multi-core thing just doesn't work ?

Perhaps is it time for you to tell what O/S, kernel, threading library you are using, and explain why do you think your application is not the problem's root cause.
No I think that multi-threading/multi-core works, it work very good on windows, but I have a problem just on linux.

My OS is Kubuntu 6.06, I use POSIX threads. Application part for multi-threading is very simple, just create two threads and wait for them to finish. Threads function is same for windows and for linux, the difference is just functions that create threads and function that wait for threads(pthread_create(...) and pthread_join() for linux, and correspond for windows). Both threads work on different data(one matrix, but one thread works on one half and another on the second) so I needn't to synchronize them.

Does anybody have any idea now?
 
Old 01-28-2007, 11:44 AM   #6
Indiestory
Member
 
Registered: Aug 2006
Location: Aberdeen, Scotland
Distribution: OpenBSD
Posts: 164
Blog Entries: 1

Rep: Reputation: 30
i have no idea what im talking about here, this is way over my head, but could it be because you arent using a smp kernel, so its just not ready to handle multithreading. ( my first google turned up something along these lines)
 
Old 01-28-2007, 05:09 PM   #7
nebojsa.andjelkovic
LQ Newbie
 
Registered: Oct 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Indiestory
i have no idea what im talking about here, this is way over my head, but could it be because you arent using a smp kernel, so its just not ready to handle multithreading. ( my first google turned up something along these lines)
No that is not problem. thanks any way.
 
Old 01-28-2007, 10:36 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
It is certainly possible for a multi-threaded application to be slower than a single-threaded one, whether there are "dual cores" or two discrete CPUs.

There are two "bright line rules" that will help you to determine if a particular application will actually benefit from a multi-threaded implementation:
  1. The functions are truly independent: The activities that you have spun-off into separate threads actually can operate independently, and can proceed "forward" in a meaningful way without reference to the others' data and/or activities.
  2. There is no hardware contention: The hardware resources which materially limit the progress of 'this' thread are unrelated to those which materially limit the progress of 'that' thread.
"Multi-core" is a sort of a halfway-compromise: there are two execution units, true, but they share the same hardware-interface to the outside world. Depending on the situation, this might be "free beer" or "big deal." Great when it's great, and worse-than-useless when it's not (but never say that to the Marketing Department!).

"Technically, 'So it goes.'"

If your application is truly designed to exploit multiple CPUs or cores, then it will truly "run faster." But if not, it may (as you have seen!) "run slower." Perhaps, much slower.
 
Old 01-29-2007, 02:00 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by nebojsa.andjelkovic
No I think that multi-threading/multi-core works, it work very good on windows, but I have a problem just on linux.
On the same hardware ?
Quote:
My OS is Kubuntu 6.06, I use POSIX threads. Application part for multi-threading is very simple, just create two threads and wait for them to finish. Threads function is same for windows and for linux, the difference is just functions that create threads and function that wait for threads(pthread_create(...) and pthread_join() for linux, and correspond for windows). Both threads work on different data(one matrix, but one thread works on one half and another on the second) so I needn't to synchronize them.
Is it pure CPU load or are there other potential resource contention (I/O, network) ?

Can you share your measurement methodology and results ?
 
Old 01-29-2007, 05:03 PM   #10
nebojsa.andjelkovic
LQ Newbie
 
Registered: Oct 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sundialsvcs
It is certainly possible for a multi-threaded application to be slower than a single-threaded one, whether there are "dual cores" or two discrete CPUs.

There are two "bright line rules" that will help you to determine if a particular application will actually benefit from a multi-threaded implementation:
  1. The functions are truly independent: The activities that you have spun-off into separate threads actually can operate independently, and can proceed "forward" in a meaningful way without reference to the others' data and/or activities.
  2. There is no hardware contention: The hardware resources which materially limit the progress of 'this' thread are unrelated to those which materially limit the progress of 'that' thread.
"Multi-core" is a sort of a halfway-compromise: there are two execution units, true, but they share the same hardware-interface to the outside world. Depending on the situation, this might be "free beer" or "big deal." Great when it's great, and worse-than-useless when it's not (but never say that to the Marketing Department!).

"Technically, 'So it goes.'"

If your application is truly designed to exploit multiple CPUs or cores, then it will truly "run faster." But if not, it may (as you have seen!) "run slower." Perhaps, much slower.

Like I alredy said on the windows It works very good. Its faster for aboutd 30-40%. So ol important things for multhithreading is enabled. So this is not a problem.
 
Old 01-29-2007, 05:11 PM   #11
nebojsa.andjelkovic
LQ Newbie
 
Registered: Oct 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlliagre
On the same hardware ?
Is it pure CPU load or are there other potential resource contention (I/O, network) ?

Can you share your measurement methodology and results ?
Yes on the same hardware. Yes pure CPU load, no I/O and network. It have just calculate an interpolation function over some matrices. Apropos it resize same picture (e.g. from 320x240 to 400x300) and one thread work on one half of picture and second works on the second.
The measurement methodology is using a system time. On the start of function I read the system time, and on the end again end just subtract them.

Any new idea for my problem?
 
Old 01-30-2007, 01:41 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
There is nothing that would explain it from the few clues you post.

Can you write a small stripped down test case demonstrating the issue and post its whole source code ?
 
Old 01-30-2007, 06:53 PM   #13
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
Quote:
Originally Posted by nebojsa.andjelkovic
Apropos it resize same picture (e.g. from 320x240 to 400x300) and one thread work on one half of picture and second works on the second.

Now I wonder if that technically counts as two independent tasks. It seems possible thats problem that could be solved more effectively with single threading. Could vary from OS to OS. Perhaps the overhead of even loading the POSIX thread library, could buckle down a simple application. It could be for linux its best to let the operating system passively handle threads for simple applications that don't truely have independent tasks. And on windows its best to take charge.
 
Old 01-30-2007, 09:56 PM   #14
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Looking at the task you are doing are the threads necessary? The time consuming part of the work would appear to be reading the data from hard-disk to RAM, then from RAM into CPU and then back to the hard disk, with the majority of the processing requiring data from RAM plus some processing, maybe there is more time waiting for the data to arrive from RAM to the CPU than actual processing. So what is your CPU load when you run this?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problems about multi-threading and/or multi-processing with tcp network in java ... alred Programming 5 06-23-2006 03:21 PM
help! perl multi-threading? eph Programming 0 05-03-2004 09:15 PM
multi-threading with signals TedMaul Programming 0 04-17-2004 07:35 PM
Multi-threading rch Programming 3 03-30-2003 10:27 PM
Multi threading Mohsen Programming 5 03-01-2003 11:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration