Hi there,
I have a simple example below showing how I create a
bitmap from a data array:
void Form1::ImageUpD(array<double,2>^ image)
{
ImageBmp = gcnew
Bitmap(image->GetLength(0),image->GetLength(1),PixelFormat::Format24bppRgb);
BitmapData^ data = ImageBmp->LockBits(Drawing::Rectangle(0,0,image->GetLength(0),image->GetLength(1)),ImageLockMode::WriteOnly,PixelFormat::Format24bppRgb);
unsigned char *bits = (unsigned char*)data->Scan0.ToPointer();
int bytesPerPixel = 3; // 3 bytes per pixel for 24 bpp rgb
int val;
for (int i = 0; i < data->Height; i++)
{
for (int j = 0; j < data->Width *
bytesPerPixel; j += bytesPerPixel)
{
val = image[j/bytesPerPixel,i];
val = ((val -
ImCLim[0])/(ImCLim[1]-ImCLim[0]))*255;
//just creating graysclae images...ImCLim is just the
[min max] of the data array
bits[j + 0] = val; // blue
bits[j + 1] = val; // green
bits[j + 2] = val; // red
}
bits += data->Stride;
}
ImageBmp->UnlockBits(data);
}
The question is: can I use 2 "background workers",
i.e., threads, to build the image from the array, one
thread on one half and the other thread the other
half. I ask because I use a dual core processor, and
even on hyper thread processors the processor usage is
only 50%. Some of the data arrays I have are quite
large, so halving the creation time would be quite
nice.
I've been looking at the background worker class in
c++.net 2005 but the usage is a little cryptic, so if
someone could exlpain a bit of it in application to
this example, thanks a lot in advance! It is
potentially a fun problem :)
Joe
Dual threaded bitmap creation
Sneh Mani Tripathi
Great, I hope you like it when it comes - there's definitely some cool stuff you can do with the pro version that's not in the express. And, yes, OpenMP is very cool ;). So long as the work in your for loops is not dependent on what happened in previous iterations of the loops you should be golden. Good luck!
Ben
ScottTarone
However, as I am now starting to try to use it, I apparently need the files. I searched my disk for omp.h, vcomp.dll and vcompd.dll, and cant find anything. I am using vc++2005 express, maybe the files aren't included I set the compiler switch and it complains that it cant find the dll's etc.
Any ideas
Thanks for you help! Appreciated.
Joe
Yoway Buorn
Unfortunately, checking the product editions page, http://msdn2.microsoft.com/en-us/library/hs24szh9.aspx, OpenMP is only included in the Professional version and above. If you can justify purchasing it, you should feel free to use OpenMP, however, I can see where purchasing an $800 retail product may not be justifiable if this is for a hobby project, rather than a professional one. There are cheaper upgrade and academic versions if you fall into those categories.
If you are sticking with the express version and want to use .Net for your threading, you should check out the documentation on threading here:
http://msdn2.microsoft.com/en-us/library/e1dx6b2h.aspx
You will also learn a lot of neat (or frustrating depending on how you look at it) stuff about concurrency by doing it this way.
What I would do is create a class implementing the Thread interface which takes an array, and a range and does your for loop on just that range of the array. Then, in your main thread I would query the number of processors available on the system, and divide up the ranges of your array based on that number. I would then launch a bunch of the for loop threads feeding in the ranges you have produced. On your main thread you can wait for all the other threads to finish, and then move on.
Good luck, and let us know how it's going if you have any questions once you've experimented a bit.
Ben
vanphuoc
6-8 weeks to deliver the trial dvd though...hope thats a worst case scenario! I would rather download it.
My project definitely started off as a hobby, but I've done so much work on it at this point, and it works just so well for its application, I have started thinking of producing and selling it. I'm sure I'm not the only enthusiast programmer to have gone through such thoughts and feelings before though eh ! I bet it happens every day! It's a small market though too...CCD image reduction and data analysis for photometry and spectroscopy in astronomy...mostly for a university setting, although amateurs might want to use it too. For some reason most astronomes like using a command line approach under Linux for doing this, and it is mostly non-visual, although you can view images it is a pain in the ***. The template I made under Matlab, before I started writing it under c++, allready sped up data reduction time from days to minutes; less programming, more science, thats the idea. Well, I needn't bore you with all this... :)
Thanks for your help Ben!
Joe
marca
Instead of using background worker threads, just use OpenMP to parallelize your loop!
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vcopenmp/html/8b54e034-9db2-4c1a-a2b1-72e14e930506.htm (OpenMP for loops)
and
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vcopenmp/html/54d8d0da-1f47-4312-9f25-5875c7dc08ed.htm (OpenMP index)
should get you started. This will help you scale well beyond two procs.
Good luck!
Ben