?
Solved

thread choice

Posted on 2006-05-02
9
Medium Priority
?
234 Views
Last Modified: 2010-04-01
Hi,

Just a general question.

I have a CPU intensive image processing task that I have to execute on like 50 images. Do you think it would be better to:

1) Let each thread process its own image - if I have 8 processors, spawn 8 threads at a time, each thread processing its own image, until all 50 are done.

2) Keep spawning threads to process multiple pixels at once - if there are 250,000 pixels per image, keep spawning 8 threads, each of which does its own per pixel processing task.

Thanks
0
Comment
Question by:minnirok
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16590527
You could run into memory trouble if you have the images processed simultaeously, but that depends on on both the size of the pictures and the memory available. It is a good idea to find a way in the middle of both extremes. If that is a 'one time job', that might be OK, but not all users will have eight processors. Basically, both approaches will work.
0
 
LVL 11

Accepted Solution

by:
dbkruger earned 1000 total points
ID: 16590579
You should definitely not multithread on a per-pixel level. The effiicient way is to process each image separately, so there are no shared memory issues, provided you have enough memory to handle the simultaneous access.

0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 100 total points
ID: 16593480
Some image processing operations work with adjacent pixels and not only with current pixel. For example, convolution filters. Dividing image to areas handled by different threads is impossible in such case.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 16593793
I second dbkruger's comment ... If you can avoid shared memory, then do it - it's a cause of many many (difficult to trace) bugs.

Just a note on spawning the threads ... If you're gonna spawn 8, then spawn them all at the start of the application, and don't end the threads untill all 50 images are processed (that way you take away the overhead of setting up and ending threads). Just let a thread retrieve the next image to be processed as soon as it has finished the previous.
0
 
LVL 7

Author Comment

by:minnirok
ID: 16605322
Hi,

Thanks for the advice so far. Another small question related to this - if I have one 1D array, can all my threads write to it concurrently as long as the other threads aren't writing to the same array index? Something like:

int g_GlobalArray[20];

void ThreadFunc1()
{
    g_GlobalArray[5] = 44;
}
void ThreadFunc2()
{
    g_GlobalArray[15] = 100;
}

There shouldn't be any problem as long as they're touching different indexes?

Thanks again
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16605668
Yes ... you're right. Different threads can use the same global array, and as long as they're not writing at the same indexes, there won't be a concurrency problem.

They can even write to the same indexes, but then you'll have to make sure you implemented proper guarding, so only one thread at a time can read/write the same variable.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 800 total points
ID: 16605755
Even though that might (in that special case) not be a problem, I'd still strongly advise to synchronize access. It won't const you much but yet will ensure that even in only slightly altered situations the functionality won't break, e.g.

int g_GlobalArray[20];
CRITICVAL_SECTION g_csGlobalArray;

void SetArrayElement(unsigned index, int val)
{
EnterCriticalSection(&g_csGlobalArray);
g_GlobalArray[index] = val;
LeaveCriticalSection(&g_csGlobalArray);
}

int GetArrayElement(unsigned index)
{
int val;
EnterCriticalSection(&g_csGlobalArray);
val = g_GlobalArray[index];
LeaveCriticalSection(&g_csGlobalArray);
}
void ThreadFunc1()
{
    SetArrayElement(5,44);
}
void ThreadFunc2()
{
    SetArrayElement(15,100);
}

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16605850
>> Even though that might (in that special case) not be a problem, I'd still strongly advise to synchronize access. It won't const you much but yet will ensure that even in only slightly altered situations the functionality won't break, e.g.

Good advice !
0
 
LVL 7

Author Comment

by:minnirok
ID: 16605881
Thank you everyone as always.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question