Register FAQ/Rules Forum Spy Search Today's Posts Mark Forums Read

Welcome to the Mac Forums forums. Please read the FAQ if you have questions. Register to participate.

 
Go Back   Mac Forums > Apple Software > Mac Programming
TouchArcade.com - iPhone Game Reviews and News

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 16, 2009, 03:11 AM   #1
Kaliemon
macrumors member
 
Join Date: Apr 2006
Send a message via AIM to Kaliemon
Simple Multithreading design

I need help with a simple multithreaded design.

I have an app that loops through each pixel of an image and calculates the data at that point. Each pixel is completely independent of the others, so there shouldn't be any synchronization issues.

Mainly, I need to find out how to spawn about 5 threads that each handle a 32x32 square, and do this for the entire image. Or possibly 5 threads each doing a pixel in the 32x32 square, I don't know which is better.

I already have a subclass of a Qthread and a method that can handle the square, I just don't know how to create the next thread when one finishes. I thought about a loop, but it waits for all 5 threads to finish before creating the next set.

I hope this all makes sense.

Note: this is done in C++ on Linux

Last edited by Kaliemon : Nov 16, 2009 at 03:50 AM.
Kaliemon is offline   Reply With Quote
Old Nov 16, 2009, 08:18 AM   #2
Sander
macrumors 6502
 
Join Date: Apr 2008
I'm not sure this is a Mac-specific question.

I wouldn't make the threads operate on square sub-images, but rather on "strips" of the image. This is better for cache coherancy. Also, it would not make much sense to spawn more threads than you have cores in your system.
Sander is offline   Reply With Quote
Old Nov 16, 2009, 08:25 AM   #3
Menge
macrumors regular
 
Join Date: Dec 2008
Location: Brazil
If you want the benefits of multi-threading, then you don't create a thread when the last one finishes. You create all threads at the same time, assign them an area (re-recommending the same as Sander: a strip of pixels) and wait for all of them to finish. Multithreading doesn't go like this:
(1)--, (2)--, (3)--, *

it goes like this:
(1) -\,
(2) --,*
(3) -/,
__________________
Menge - 2.0GHz Unibody MacBook, 4GB RAM, 160GB HD - 16GB iPhone (unlocked)
Menge is offline   Reply With Quote
Old Nov 16, 2009, 12:35 PM   #4
Kaliemon
Thread Starter
macrumors member
 
Join Date: Apr 2006
Send a message via AIM to Kaliemon
So if I have a 4 core system, and say a 500px high image, would I create 500 threads and let the OS handle the queueing?

I was thinking it would be best to have 4 threads processing, a 5th waiting to run and every time the 5th thread starts create another thread to be waiting to run. I am trying to break up the threads to process the smallest amount of data since 3 could finish instantly and I don't won't the last running thread to have to do a large amount of work when there are available threads.

Also with the square vs line choices, the data in our case is much more similar in a 32x32 patch vs across a line, so wouldn't it be better for cache?
Kaliemon is offline   Reply With Quote
Old Nov 16, 2009, 12:54 PM   #5
savar
macrumors 68000
 
savar's Avatar
 
Join Date: Jun 2003
Location: District of Columbia
Send a message via AIM to savar
Quote:
Originally Posted by Kaliemon View Post
So if I have a 4 core system, and say a 500px high image, would I create 500 threads and let the OS handle the queueing?
Definitely DO NOT create 500 threads. That's too much for any current computer.

The Snow Leopard way to do this is to create 500 blocks, and let GCD decide how many threads to create and when to schedule them.
__________________
Mehce
savar is offline   Reply With Quote
Old Nov 16, 2009, 12:57 PM   #6
Kaliemon
Thread Starter
macrumors member
 
Join Date: Apr 2006
Send a message via AIM to Kaliemon
Quote:
Originally Posted by savar View Post
The Snow Leopard way to do this is to create 500 blocks, and let GCD decide how many threads to create and when to schedule them.
That's how I am used to doing things, but since this has to run on linux I don't have GCD.
Kaliemon is offline   Reply With Quote
Old Nov 16, 2009, 01:18 PM   #7
Catfish_Man
macrumors 68020
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Threadsafe queue implementations shouldn't be *too* hard to find. Once you have one, you can simply spawn N threads where N is the number of cpus available, then have them sit in while (pixelstrip = workqueue.pop()) { process(pixelstrip); } loops. You may have to fiddle with the granularity of the pixel strips for optimum efficiency.
Catfish_Man is offline   Reply With Quote

Reply

Mac Forums > Apple Software > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 06:40 PM.

Mac News | Mac Rumors | iPhone Game Reviews | iPhone Apps

Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 2002-2010, MacRumors.com, LLC