15 |
#define YSIZE 500 |
#define YSIZE 500 |
16 |
#include "algorithm.h" |
#include "algorithm.h" |
17 |
|
|
18 |
|
BOOL VERBOSE = FALSE; |
19 |
|
|
20 |
|
|
21 |
/* BMP Header */ |
/* BMP Header */ |
22 |
unsigned char header[54]={0x42,0x4d, // signature BM |
unsigned char header[54]={0x42,0x4d, // signature BM |
96 |
} |
} |
97 |
|
|
98 |
|
|
99 |
|
// arguments for each thread |
100 |
|
typedef struct _WorkerArguments { |
101 |
|
int start_row; |
102 |
|
int number_of_rows; |
103 |
|
unsigned char * pBitmap; |
104 |
|
} WORKERARGS, *PWORKERARGS; |
105 |
|
|
106 |
|
// worker thread - main entry function |
107 |
|
DWORD WINAPI fractal_thread (LPVOID lpParam) { |
108 |
|
|
109 |
|
// thread stuff |
110 |
|
int thread_id; |
111 |
|
PWORKERARGS args; |
112 |
|
unsigned char *pDataBitmapSegment; |
113 |
|
|
114 |
|
// fractal calculation |
115 |
|
int x, y; |
116 |
|
char bgr[3]; |
117 |
|
|
118 |
|
thread_id = GetCurrentThreadId(); |
119 |
|
|
120 |
|
// get worker arguments |
121 |
|
args = (PWORKERARGS)lpParam; |
122 |
|
|
123 |
|
// calculate pointer to beginning of segment |
124 |
|
pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE); |
125 |
|
|
126 |
|
// debugging |
127 |
|
if (VERBOSE) { |
128 |
|
printf("----------------------------------------------\n"); |
129 |
|
printf("thread_id: %i\n", thread_id); |
130 |
|
printf("arg.start_row: %i\n", args->start_row); |
131 |
|
printf("arg.number_of_rows: %i\n", args->number_of_rows); |
132 |
|
printf("segment_start: %p\n", pDataBitmapSegment); |
133 |
|
} |
134 |
|
|
135 |
|
// calculate fractal |
136 |
|
for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { |
137 |
|
//printf("calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); |
138 |
|
for (x = 0; x < XSIZE; x++) { |
139 |
|
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
140 |
|
|
141 |
|
// transfer color values to current pixel |
142 |
|
pDataBitmapSegment[0] = bgr[0]; |
143 |
|
pDataBitmapSegment[1] = bgr[1]; |
144 |
|
pDataBitmapSegment[2] = bgr[2]; |
145 |
|
|
146 |
|
// move pointer to next pixel |
147 |
|
pDataBitmapSegment += 3; |
148 |
|
|
149 |
|
} |
150 |
|
//no padding required because 1500%4 =0 ??? |
151 |
|
} |
152 |
|
|
153 |
|
if (VERBOSE) |
154 |
|
printf("thread finished: %i\n", thread_id); |
155 |
|
return 0; |
156 |
|
|
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
int main(int argc, char *argv[]) { |
int main(int argc, char *argv[]) { |
161 |
|
|
162 |
// MMF support |
// MMF support |
163 |
DWORD err; |
DWORD err; |
164 |
HANDLE hMap, hFile; |
HANDLE hMap, hFile; |
165 |
LPVOID pData; |
LPVOID pData; |
166 |
unsigned char *pDataBitmap, *pDataBitmapCurrent; |
unsigned char *pDataBitmap; |
167 |
|
|
168 |
// workers |
// workers |
169 |
unsigned int workers = 3; |
int workers = 50; |
170 |
unsigned int worker_index, worker_rows, worker_startrow; |
int worker_index, worker_rows, worker_startrow; |
171 |
|
HANDLE *worker_handles; |
172 |
// fractal calculation |
PWORKERARGS worker_args; |
173 |
int x, y; |
|
|
char bgr[3]; |
|
|
|
|
174 |
|
|
175 |
// create empty bmp-file (black background) |
// create empty bmp-file (black background) |
176 |
write_blank_file("test.bmp"); |
write_blank_file("test.bmp"); |
199 |
pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); |
pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); |
200 |
|
|
201 |
// debugging |
// debugging |
202 |
printf("pos. of file: %p\n", pData); |
if (VERBOSE) { |
203 |
printf("pos. of bitmap: %p\n", pDataBitmap); |
printf("pos. of file: %p\n", pData); |
204 |
|
printf("pos. of bitmap: %p\n", pDataBitmap); |
205 |
// pointer to current pixel |
} |
|
pDataBitmapCurrent = pDataBitmap; |
|
206 |
|
|
207 |
/* |
/* |
208 |
// turn bitmap into white wand |
// turn bitmap into white canvas |
209 |
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
210 |
//pDataBitmap[offset] = 255; |
*pDataBitmap = 255; |
211 |
*pDataBitmapCurrent = 255; |
pDataBitmap++; |
|
pDataBitmapCurrent++; |
|
212 |
} |
} |
213 |
|
exit(0); |
214 |
*/ |
*/ |
215 |
|
|
216 |
// calculate segments of bitmap for worker threads/processes |
// allocate memory for bitmap |
217 |
|
/* |
218 |
|
if ((pDataBitmap = malloc(XSIZE * YSIZE * 3 * sizeof(pDataBitmap[0]))) == NULL) |
219 |
|
perror("malloc"), exit(1); |
220 |
|
*/ |
221 |
|
|
222 |
|
// allocate memory for table of all worker handles |
223 |
|
if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL) |
224 |
|
perror("malloc"), exit(1); |
225 |
|
|
226 |
|
// allocate memory for table of all worker arguments |
227 |
|
if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL) |
228 |
|
perror("malloc"), exit(1); |
229 |
|
|
230 |
|
|
231 |
|
// calculate segments of bitmap for worker threads/processes and start them |
232 |
worker_rows = YSIZE / workers; |
worker_rows = YSIZE / workers; |
233 |
printf("rows for each worker: %i\n", worker_rows); |
if (VERBOSE) |
234 |
|
printf("rows for each worker: %i\n", worker_rows); |
235 |
for (worker_index = 0; worker_index < workers; worker_index++) { |
for (worker_index = 0; worker_index < workers; worker_index++) { |
236 |
|
|
237 |
|
// debugging: just run single thread |
238 |
|
//if (worker_index == 1) |
239 |
|
// continue; |
240 |
|
|
241 |
// number of row to start for each worker |
// number of row to start for each worker |
242 |
worker_startrow = worker_index * worker_rows; |
worker_startrow = worker_index * worker_rows; |
243 |
|
|
244 |
// recalculate number of rows for last worker if (YSIZE mod workers) != 0 |
// recalculate number of rows for last worker if (YSIZE mod workers) != 0 |
245 |
if (worker_index == workers - 1) { |
if (worker_index == workers - 1) { |
246 |
worker_rows = YSIZE - worker_startrow; |
worker_rows = YSIZE - worker_startrow; |
247 |
printf("rows for last worker: %i\n", worker_rows); |
if (VERBOSE) |
248 |
|
printf("rows for last worker: %i\n", worker_rows); |
249 |
} |
} |
250 |
|
|
251 |
|
worker_args[worker_index].start_row = worker_startrow; |
252 |
|
worker_args[worker_index].number_of_rows = worker_rows; |
253 |
|
worker_args[worker_index].pBitmap = pDataBitmap; |
254 |
|
|
255 |
|
worker_handles[worker_index] = CreateThread( |
256 |
|
NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes, |
257 |
|
0, // SIZE_T dwStackSize, |
258 |
|
&fractal_thread, // LPTHREAD_START_ROUTINE lpStartAddress, |
259 |
|
&worker_args[worker_index], // LPVOID lpParameter, |
260 |
|
0, // DWORD dwCreationFlags, |
261 |
|
NULL // LPDWORD lpThreadId |
262 |
|
); |
263 |
|
|
264 |
} |
} |
265 |
|
|
266 |
// calculate fractal |
// wait for all threads |
267 |
for (y=YSIZE-1; y>=0; y--) { |
if (VERBOSE) |
268 |
for (x=0; x<XSIZE; x++) { |
printf("waiting...\n"); |
269 |
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
270 |
|
perror("WaitForMultipleObjects"); |
271 |
// debugging |
|
272 |
//printf("pointer: %p\n", pDataBitmapCurrent); |
// debugging: just run single thread |
273 |
|
//if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) |
274 |
// transfer color values to current pixel |
// perror("WaitForSingleObject"); |
275 |
pDataBitmapCurrent[0] = bgr[0]; |
|
276 |
pDataBitmapCurrent[1] = bgr[1]; |
// close all worker handles |
277 |
pDataBitmapCurrent[2] = bgr[2]; |
for (worker_index = 0; worker_index < workers; worker_index++) |
278 |
|
CloseHandle(worker_handles[worker_index]); |
279 |
// move pointer to next pixel |
|
|
pDataBitmapCurrent += 3; |
|
|
|
|
|
} |
|
|
//no padding required because 1500%4 =0 |
|
|
} |
|
|
|
|
|
|
|
280 |
/* write the result into the file */ |
/* write the result into the file */ |
281 |
if (!FlushViewOfFile(pData, 0)) { |
if (!FlushViewOfFile(pData, 0)) { |
282 |
err = GetLastError(); |
err = GetLastError(); |
296 |
printErrorAndExit("Error at CloseHandle", err); |
printErrorAndExit("Error at CloseHandle", err); |
297 |
} |
} |
298 |
|
|
299 |
|
free(worker_args); |
300 |
|
free(worker_handles); |
301 |
|
|
302 |
|
return 0; |
303 |
|
|
304 |
} |
} |