15 |
#define YSIZE 500 |
#define YSIZE 500 |
16 |
#include "algorithm.h" |
#include "algorithm.h" |
17 |
|
|
18 |
#define MASTER -1 |
BOOL VERBOSE = FALSE; |
|
|
|
|
BOOL VERBOSE = TRUE; |
|
19 |
|
|
20 |
|
|
21 |
/* BMP Header */ |
/* BMP Header */ |
49 |
0, |
0, |
50 |
NULL )) |
NULL )) |
51 |
{ |
{ |
52 |
fprintf(stderr,"%s : %s\n",msg,lpMsgBuf); |
fprintf(stdout, "%s: %s\n", msg, lpMsgBuf); |
53 |
LocalFree(lpMsgBuf); |
LocalFree(lpMsgBuf); |
54 |
} |
} |
55 |
else |
else |
56 |
{ |
{ |
57 |
fprintf(stderr,"Error at FormatMesage: %d\n",err=GetLastError()); |
fprintf(stdout, "Error at FormatMesage: %d\n",err=GetLastError()); |
58 |
} |
} |
59 |
exit(err); |
exit(err); |
60 |
} |
} |
125 |
|
|
126 |
// debugging |
// debugging |
127 |
if (VERBOSE) { |
if (VERBOSE) { |
128 |
printf("----------------------------------------------\n"); |
fprintf(stdout, "----------------------------------------------\n"); |
129 |
printf("thread_id: %i\n", thread_id); |
fprintf(stdout, "thread_id: %i\n", thread_id); |
130 |
printf("arg.start_row: %i\n", args->start_row); |
fprintf(stdout, "arg.start_row: %i\n", args->start_row); |
131 |
printf("arg.number_of_rows: %i\n", args->number_of_rows); |
fprintf(stdout, "arg.number_of_rows: %i\n", args->number_of_rows); |
132 |
printf("segment_start: %p\n", pDataBitmapSegment); |
fprintf(stdout, "segment_start: %p\n", pDataBitmapSegment); |
133 |
} |
} |
134 |
|
|
135 |
// calculate fractal |
// calculate fractal |
136 |
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--) { |
137 |
//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); |
138 |
for (x = 0; x < XSIZE; x++) { |
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]); |
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
140 |
|
|
151 |
} |
} |
152 |
|
|
153 |
if (VERBOSE) |
if (VERBOSE) |
154 |
printf("thread finished: %i\n", thread_id); |
fprintf(stdout, "thread finished: %i\n", thread_id); |
155 |
return 0; |
return 0; |
156 |
|
|
157 |
} |
} |
173 |
|
|
174 |
// threads or processes? |
// threads or processes? |
175 |
BOOL use_processes = TRUE; |
BOOL use_processes = TRUE; |
176 |
int worker_id = MASTER; |
BOOL is_worker_process = FALSE; |
177 |
|
|
178 |
// information for creating processes |
// information for creating processes |
179 |
STARTUPINFO si; |
STARTUPINFO si; |
181 |
char szCmdline[1024]; |
char szCmdline[1024]; |
182 |
|
|
183 |
|
|
184 |
|
VERBOSE = TRUE; |
185 |
|
|
186 |
// "parse" command line arguments |
// "parse" command line arguments |
187 |
if (argc >= 2) { |
if (argc >= 2) { |
188 |
worker_id = atoi(argv[1]); |
if (strcmp(argv[1], "--worker") == 0) { |
189 |
|
is_worker_process = TRUE; |
190 |
|
} |
191 |
} |
} |
192 |
|
|
193 |
if (VERBOSE) { |
if (VERBOSE && use_processes) { |
194 |
printf("==============================================================\n"); |
fprintf(stdout, "===================================================== "); |
195 |
printf("worker-id: %i\n", worker_id); |
if (is_worker_process) |
196 |
|
fprintf(stdout, "WORKER-PROCESS\n"); |
197 |
|
else |
198 |
|
fprintf(stdout, "MASTER-PROCESS\n"); |
199 |
} |
} |
200 |
|
|
201 |
// create empty bmp-file (black background) |
// create empty bmp-file (black background) |
202 |
if (worker_id == MASTER) |
if (!is_worker_process) |
203 |
write_blank_file("test.bmp"); |
write_blank_file("test.bmp"); |
204 |
|
|
205 |
if (worker_id == MASTER) { |
// master creates memory mapped file |
206 |
|
if (!is_worker_process) { |
207 |
|
|
208 |
/* open file for reading and writing */ |
// open file for reading and writing |
209 |
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); |
210 |
if (INVALID_HANDLE_VALUE == hFile) { |
if (INVALID_HANDLE_VALUE == hFile) { |
211 |
err = GetLastError(); |
err = GetLastError(); |
212 |
printErrorAndExit("Error at CreateFile",err); |
printErrorAndExit("Error at CreateFile",err); |
213 |
} |
} |
214 |
|
|
215 |
/* create the file mapping object */ |
// create the file mapping object |
216 |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
217 |
if (NULL == hMap) { |
if (NULL == hMap) { |
218 |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
219 |
} |
} |
220 |
|
|
221 |
|
// worker uses existing memory mapped file |
222 |
} else { |
} else { |
223 |
|
|
224 |
// open existing mapping object |
// open existing mapping object |
227 |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
228 |
} |
} |
229 |
|
|
230 |
/* map the whole file into the process context */ |
// map the whole file into the process context |
231 |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
232 |
if (NULL == pData) { |
if (NULL == pData) { |
233 |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
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_process) { |
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 |
|
|
263 |
worker_args[0].pBitmap = pDataBitmap; |
worker_args[0].pBitmap = pDataBitmap; |
264 |
|
|
265 |
fractal_create_segment(&worker_args[0]); |
fractal_create_segment(&worker_args[0]); |
266 |
|
|
267 |
|
// cleanup mmap-handle |
268 |
|
if (!CloseHandle(hMap)) |
269 |
|
printErrorAndExit("Error at CloseHandle", GetLastError()); |
270 |
|
|
271 |
return 0; |
return 0; |
272 |
} |
} |
298 |
// calculate bitmap segment length for workers |
// calculate bitmap segment length for workers |
299 |
worker_rows = YSIZE / workers; |
worker_rows = YSIZE / workers; |
300 |
if (VERBOSE) |
if (VERBOSE) |
301 |
printf("rows for each worker: %i\n", worker_rows); |
fprintf(stdout, "rows for each worker: %i\n", worker_rows); |
302 |
|
|
303 |
// start workers |
// start workers |
304 |
for (worker_index = 0; worker_index < workers; worker_index++) { |
for (worker_index = 0; worker_index < workers; worker_index++) { |
314 |
if (worker_index == workers - 1) { |
if (worker_index == workers - 1) { |
315 |
worker_rows = YSIZE - worker_startrow; |
worker_rows = YSIZE - worker_startrow; |
316 |
if (VERBOSE) |
if (VERBOSE) |
317 |
printf("rows for last worker: %i\n", worker_rows); |
fprintf(stdout, "rows for last worker: %i\n", worker_rows); |
318 |
} |
} |
319 |
|
|
320 |
// assign each worker's arguments |
// assign each worker's arguments |
338 |
|
|
339 |
} else { |
} else { |
340 |
|
|
341 |
_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); |
342 |
|
if (VERBOSE) |
343 |
|
fprintf(stdout, "starting worker process: %s\n", szCmdline); |
344 |
ZeroMemory( &si, sizeof(si) ); |
ZeroMemory( &si, sizeof(si) ); |
345 |
si.cb = sizeof(si); |
si.cb = sizeof(si); |
346 |
ZeroMemory( &pi, sizeof(pi) ); |
ZeroMemory( &pi, sizeof(pi) ); |
367 |
|
|
368 |
// wait for all threads |
// wait for all threads |
369 |
if (VERBOSE) |
if (VERBOSE) |
370 |
printf("waiting for workers to finish...\n"); |
fprintf(stdout, "waiting for workers to finish...\n"); |
371 |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
372 |
perror("WaitForMultipleObjects"); |
perror("WaitForMultipleObjects"); |
373 |
|
|
379 |
for (worker_index = 0; worker_index < workers; worker_index++) |
for (worker_index = 0; worker_index < workers; worker_index++) |
380 |
CloseHandle(worker_handles[worker_index]); |
CloseHandle(worker_handles[worker_index]); |
381 |
|
|
382 |
/* write the result into the file */ |
// write the result into the file |
383 |
if (!FlushViewOfFile(pData, 0)) { |
if (!FlushViewOfFile(pData, 0)) { |
384 |
err = GetLastError(); |
err = GetLastError(); |
385 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
386 |
} |
} |
387 |
|
|
388 |
/* remove the mapped file */ |
// remove the mapped file |
389 |
if (!UnmapViewOfFile(pData)) { |
if (!UnmapViewOfFile(pData)) { |
390 |
err = GetLastError(); |
err = GetLastError(); |
391 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
392 |
exit(err); |
exit(err); |
393 |
} |
} |
394 |
|
|
395 |
/* cleanup handles */ |
// cleanup handles |
396 |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
397 |
err = GetLastError(); |
err = GetLastError(); |
398 |
printErrorAndExit("Error at CloseHandle", err); |
printErrorAndExit("Error at CloseHandle", err); |