• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

thread choice

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
minnirok
Asked:
minnirok
  • 3
  • 2
  • 2
  • +2
4 Solutions
 
jkrCommented:
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
 
dbkrugerCommented:
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
 
AlexFMCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Infinity08Commented:
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
 
minnirokAuthor Commented:
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
 
Infinity08Commented:
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
 
jkrCommented:
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
 
Infinity08Commented:
>> 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
 
minnirokAuthor Commented:
Thank you everyone as always.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now