15 |
#define YSIZE 500 |
#define YSIZE 500 |
16 |
#include "algorithm.h" |
#include "algorithm.h" |
17 |
|
|
18 |
#define MASTER -1 |
#define VERBOSE_HANDLE "stderr"; |
19 |
|
|
20 |
BOOL VERBOSE = TRUE; |
BOOL VERBOSE = FALSE; |
21 |
|
|
22 |
|
|
23 |
/* BMP Header */ |
/* BMP Header */ |
51 |
0, |
0, |
52 |
NULL )) |
NULL )) |
53 |
{ |
{ |
54 |
fprintf(stderr,"%s : %s\n",msg,lpMsgBuf); |
fprintf(stdout, "%s: %s\n", msg, lpMsgBuf); |
55 |
LocalFree(lpMsgBuf); |
LocalFree(lpMsgBuf); |
56 |
} |
} |
57 |
else |
else |
58 |
{ |
{ |
59 |
fprintf(stderr,"Error at FormatMesage: %d\n",err=GetLastError()); |
fprintf(stdout, "Error at FormatMesage: %d\n",err=GetLastError()); |
60 |
} |
} |
61 |
exit(err); |
exit(err); |
62 |
} |
} |
127 |
|
|
128 |
// debugging |
// debugging |
129 |
if (VERBOSE) { |
if (VERBOSE) { |
130 |
printf("----------------------------------------------\n"); |
fprintf(stdout, "----------------------------------------------\n"); |
131 |
printf("thread_id: %i\n", thread_id); |
fprintf(stdout, "thread_id: %i\n", thread_id); |
132 |
printf("arg.start_row: %i\n", args->start_row); |
fprintf(stdout, "arg.start_row: %i\n", args->start_row); |
133 |
printf("arg.number_of_rows: %i\n", args->number_of_rows); |
fprintf(stdout, "arg.number_of_rows: %i\n", args->number_of_rows); |
134 |
printf("segment_start: %p\n", pDataBitmapSegment); |
fprintf(stdout, "segment_start: %p\n", pDataBitmapSegment); |
135 |
} |
} |
136 |
|
|
137 |
// calculate fractal |
// calculate fractal |
138 |
for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { |
for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { |
139 |
//printf("calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); |
//fprintf(stdout, "calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); |
140 |
for (x = 0; x < XSIZE; x++) { |
for (x = 0; x < XSIZE; x++) { |
141 |
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
142 |
|
|
153 |
} |
} |
154 |
|
|
155 |
if (VERBOSE) |
if (VERBOSE) |
156 |
printf("thread finished: %i\n", thread_id); |
fprintf(stdout, "thread finished: %i\n", thread_id); |
157 |
return 0; |
return 0; |
158 |
|
|
159 |
} |
} |
175 |
|
|
176 |
// threads or processes? |
// threads or processes? |
177 |
BOOL use_processes = TRUE; |
BOOL use_processes = TRUE; |
178 |
int worker_id = MASTER; |
BOOL is_worker = FALSE; |
179 |
|
|
180 |
// information for creating processes |
// information for creating processes |
181 |
STARTUPINFO si; |
STARTUPINFO si; |
183 |
char szCmdline[1024]; |
char szCmdline[1024]; |
184 |
|
|
185 |
|
|
186 |
|
VERBOSE = TRUE; |
187 |
|
|
188 |
// "parse" command line arguments |
// "parse" command line arguments |
189 |
if (argc >= 2) { |
if (argc >= 2) { |
190 |
worker_id = atoi(argv[1]); |
if (strcmp(argv[1], "--worker") == 0) { |
191 |
|
is_worker = TRUE; |
192 |
|
} |
193 |
} |
} |
194 |
|
|
195 |
if (VERBOSE) { |
if (VERBOSE && use_processes) { |
196 |
printf("==============================================================\n"); |
fprintf(stdout, "===================================================== "); |
197 |
printf("worker-id: %i\n", worker_id); |
if (is_worker) |
198 |
|
fprintf(stdout, "WORKER-PROCESS\n"); |
199 |
|
else |
200 |
|
fprintf(stdout, "MASTER-PROCESS\n"); |
201 |
} |
} |
202 |
|
|
203 |
// create empty bmp-file (black background) |
// create empty bmp-file (black background) |
204 |
if (worker_id == MASTER) |
if (!is_worker) |
205 |
write_blank_file("test.bmp"); |
write_blank_file("test.bmp"); |
206 |
|
|
207 |
if (worker_id == MASTER) { |
if (!is_worker) { |
208 |
|
|
209 |
/* open file for reading and writing */ |
/* open file for reading and writing */ |
210 |
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
239 |
|
|
240 |
// debugging |
// debugging |
241 |
if (VERBOSE) { |
if (VERBOSE) { |
242 |
printf("pos. of file: %p\n", pData); |
fprintf(stdout, "pos. of file: %p\n", pData); |
243 |
printf("pos. of bitmap: %p\n", pDataBitmap); |
fprintf(stdout, "pos. of bitmap: %p\n", pDataBitmap); |
244 |
} |
} |
245 |
|
|
246 |
|
|
247 |
if (use_processes && worker_id != MASTER) { |
if (use_processes && is_worker) { |
248 |
|
|
249 |
if (VERBOSE) |
if (VERBOSE) |
250 |
printf("inside worker-process\n"); |
fprintf(stdout, "inside worker-process\n"); |
|
|
|
|
worker_index = worker_id; |
|
251 |
|
|
252 |
|
// get segment information from command line |
253 |
worker_startrow = atoi(argv[2]); |
worker_startrow = atoi(argv[2]); |
254 |
worker_rows = atoi(argv[3]); |
worker_rows = atoi(argv[3]); |
255 |
|
|
294 |
// calculate bitmap segment length for workers |
// calculate bitmap segment length for workers |
295 |
worker_rows = YSIZE / workers; |
worker_rows = YSIZE / workers; |
296 |
if (VERBOSE) |
if (VERBOSE) |
297 |
printf("rows for each worker: %i\n", worker_rows); |
fprintf(stdout, "rows for each worker: %i\n", worker_rows); |
298 |
|
|
299 |
// start workers |
// start workers |
300 |
for (worker_index = 0; worker_index < workers; worker_index++) { |
for (worker_index = 0; worker_index < workers; worker_index++) { |
310 |
if (worker_index == workers - 1) { |
if (worker_index == workers - 1) { |
311 |
worker_rows = YSIZE - worker_startrow; |
worker_rows = YSIZE - worker_startrow; |
312 |
if (VERBOSE) |
if (VERBOSE) |
313 |
printf("rows for last worker: %i\n", worker_rows); |
fprintf(stdout, "rows for last worker: %i\n", worker_rows); |
314 |
} |
} |
315 |
|
|
316 |
// assign each worker's arguments |
// assign each worker's arguments |
334 |
|
|
335 |
} else { |
} else { |
336 |
|
|
337 |
_snprintf(szCmdline, 1023, "%s %i %i %i", argv[0], worker_index + 1, worker_startrow, worker_rows); |
_snprintf(szCmdline, 1023, "%s %s %i %i", argv[0], "--worker", worker_startrow, worker_rows); |
338 |
|
if (VERBOSE) |
339 |
|
fprintf(stdout, "starting worker process: %s\n", szCmdline); |
340 |
ZeroMemory( &si, sizeof(si) ); |
ZeroMemory( &si, sizeof(si) ); |
341 |
si.cb = sizeof(si); |
si.cb = sizeof(si); |
342 |
ZeroMemory( &pi, sizeof(pi) ); |
ZeroMemory( &pi, sizeof(pi) ); |
363 |
|
|
364 |
// wait for all threads |
// wait for all threads |
365 |
if (VERBOSE) |
if (VERBOSE) |
366 |
printf("waiting for workers to finish...\n"); |
fprintf(stdout, "waiting for workers to finish...\n"); |
367 |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
368 |
perror("WaitForMultipleObjects"); |
perror("WaitForMultipleObjects"); |
369 |
|
|