| 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 |
} |
} |
| 104 |
} WORKERARGS, *PWORKERARGS; |
} WORKERARGS, *PWORKERARGS; |
| 105 |
|
|
| 106 |
// worker thread - main entry function |
// worker thread - main entry function |
| 107 |
DWORD WINAPI fractal_thread (LPVOID lpParam) { |
DWORD WINAPI fractal_create_segment (LPVOID lpParam) { |
| 108 |
|
|
| 109 |
// thread stuff |
// thread stuff |
| 110 |
int thread_id; |
int thread_id; |
| 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 |
} |
} |
| 158 |
|
|
| 159 |
|
BOOL scan_argv(int argc, char *argv[], char opt_name[], char *opt_value[]) { |
| 160 |
|
int i; |
| 161 |
|
char * opt_current_name; |
| 162 |
|
char * opt_current_value; |
| 163 |
|
|
| 164 |
|
//printf("searching for: '%s'\n", opt_name); |
| 165 |
|
|
| 166 |
|
for (i = 1; i < argc; i++) { |
| 167 |
|
opt_current_name = argv[i]; |
| 168 |
|
if (strcmp(opt_current_name, opt_name) == 0) { |
| 169 |
|
//printf("opt: %s\n", opt_current_name); |
| 170 |
|
opt_current_value = argv[i+1]; |
| 171 |
|
//printf("opt-current-value: %s\n", opt_current_value); |
| 172 |
|
if (opt_current_value != NULL) { |
| 173 |
|
//*opt_value = *argv[i+1]; |
| 174 |
|
*opt_value = *opt_current_value; |
| 175 |
|
//printf("opt-value: %s\n", opt_value); |
| 176 |
|
} |
| 177 |
|
return TRUE; |
| 178 |
|
} |
| 179 |
|
} |
| 180 |
|
return FALSE; |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
| 184 |
int main(int argc, char *argv[]) { |
int main(int argc, char *argv[]) { |
| 185 |
|
|
| 190 |
unsigned char *pDataBitmap; |
unsigned char *pDataBitmap; |
| 191 |
|
|
| 192 |
// workers |
// workers |
| 193 |
int workers = 50; |
int workers = 5; |
| 194 |
int worker_index, worker_rows, worker_startrow; |
int worker_index, worker_rows, worker_startrow; |
| 195 |
HANDLE *worker_handles; |
HANDLE *worker_handles; |
| 196 |
PWORKERARGS worker_args; |
PWORKERARGS worker_args; |
| 197 |
|
|
| 198 |
|
// threads or processes? |
| 199 |
|
BOOL use_processes = FALSE; |
| 200 |
|
BOOL is_worker_process = FALSE; |
| 201 |
|
|
| 202 |
|
// information for creating processes |
| 203 |
|
STARTUPINFO si; |
| 204 |
|
PROCESS_INFORMATION pi; |
| 205 |
|
char szCmdline[65536]; |
| 206 |
|
|
| 207 |
|
// command line stuff |
| 208 |
|
char arg_option[1024]; |
| 209 |
|
char *arg_value[1024]; |
| 210 |
|
//arg_value = malloc(1024); |
| 211 |
|
|
| 212 |
|
VERBOSE = FALSE; |
| 213 |
|
|
| 214 |
|
// "parse" command line arguments |
| 215 |
|
/* |
| 216 |
|
if (argc >= 2) { |
| 217 |
|
if (strcmp(argv[1], "--worker") == 0) { |
| 218 |
|
is_worker_process = TRUE; |
| 219 |
|
} |
| 220 |
|
} |
| 221 |
|
*/ |
| 222 |
|
|
| 223 |
|
//scan_argv(argc, argv, "--worker", &arg_value); |
| 224 |
|
//*arg_value = '\0'; |
| 225 |
|
// parse command line arguments |
| 226 |
|
if (scan_argv(argc, argv, "--worker", arg_value)) { |
| 227 |
|
use_processes = TRUE; |
| 228 |
|
is_worker_process = TRUE; |
| 229 |
|
|
| 230 |
|
} else if (scan_argv(argc, argv, "-p", arg_value)) { |
| 231 |
|
use_processes = TRUE; |
| 232 |
|
is_worker_process = FALSE; |
| 233 |
|
workers = atoi(arg_value); |
| 234 |
|
|
| 235 |
|
} else if (scan_argv(argc, argv, "-t", arg_value)) { |
| 236 |
|
use_processes = FALSE; |
| 237 |
|
is_worker_process = FALSE; |
| 238 |
|
workers = atoi(arg_value); |
| 239 |
|
|
| 240 |
|
} |
| 241 |
|
//printf("value: %s\n", arg_value); |
| 242 |
|
//exit(0); |
| 243 |
|
|
| 244 |
|
|
| 245 |
|
if (VERBOSE && use_processes) { |
| 246 |
|
fprintf(stdout, "===================================================== "); |
| 247 |
|
if (is_worker_process) |
| 248 |
|
fprintf(stdout, "WORKER-PROCESS\n"); |
| 249 |
|
else |
| 250 |
|
fprintf(stdout, "MASTER-PROCESS\n"); |
| 251 |
|
} |
| 252 |
|
|
| 253 |
// create empty bmp-file (black background) |
// create empty bmp-file (black background) |
| 254 |
write_blank_file("test.bmp"); |
if (!is_worker_process) |
| 255 |
|
write_blank_file("test.bmp"); |
| 256 |
|
|
| 257 |
/* open file for reading and writing */ |
// master creates memory mapped file |
| 258 |
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
if (!is_worker_process) { |
| 259 |
if (INVALID_HANDLE_VALUE == hFile) { |
|
| 260 |
err = GetLastError(); |
// open file for reading and writing |
| 261 |
printErrorAndExit("Error at CreateFile",err); |
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 262 |
} |
if (INVALID_HANDLE_VALUE == hFile) { |
| 263 |
|
err = GetLastError(); |
| 264 |
|
printErrorAndExit("Error at CreateFile",err); |
| 265 |
|
} |
| 266 |
|
|
| 267 |
/* create the file mapping object */ |
// create the file mapping object |
| 268 |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
| 269 |
if (NULL == hMap) { |
if (NULL == hMap) { |
| 270 |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
| 271 |
|
} |
| 272 |
|
|
| 273 |
|
// worker uses existing memory mapped file |
| 274 |
|
} else { |
| 275 |
|
|
| 276 |
|
// open existing mapping object |
| 277 |
|
hMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, "bmp_fractal"); |
| 278 |
|
if (NULL == hMap) |
| 279 |
|
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
| 280 |
} |
} |
| 281 |
|
|
| 282 |
/* map the whole file into the process context */ |
// map the whole file into the process context |
| 283 |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
| 284 |
if (NULL == pData) { |
if (NULL == pData) { |
| 285 |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
| 291 |
|
|
| 292 |
// debugging |
// debugging |
| 293 |
if (VERBOSE) { |
if (VERBOSE) { |
| 294 |
printf("pos. of file: %p\n", pData); |
fprintf(stdout, "pos. of file: %p\n", pData); |
| 295 |
printf("pos. of bitmap: %p\n", pDataBitmap); |
fprintf(stdout, "pos. of bitmap: %p\n", pDataBitmap); |
| 296 |
} |
} |
| 297 |
|
|
| 298 |
|
|
| 299 |
|
if (use_processes && is_worker_process) { |
| 300 |
|
|
| 301 |
|
if (VERBOSE) |
| 302 |
|
fprintf(stdout, "inside worker-process\n"); |
| 303 |
|
|
| 304 |
|
// get segment information from command line |
| 305 |
|
worker_startrow = atoi(argv[2]); |
| 306 |
|
worker_rows = atoi(argv[3]); |
| 307 |
|
|
| 308 |
|
// allocate memory for one worker's arguments |
| 309 |
|
if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL) |
| 310 |
|
perror("malloc"), exit(1); |
| 311 |
|
|
| 312 |
|
// assign worker's arguments |
| 313 |
|
worker_args[0].start_row = worker_startrow; |
| 314 |
|
worker_args[0].number_of_rows = worker_rows; |
| 315 |
|
worker_args[0].pBitmap = pDataBitmap; |
| 316 |
|
|
| 317 |
|
fractal_create_segment(&worker_args[0]); |
| 318 |
|
|
| 319 |
|
// cleanup mmap-handle |
| 320 |
|
if (!CloseHandle(hMap)) |
| 321 |
|
printErrorAndExit("Error at CloseHandle", GetLastError()); |
| 322 |
|
|
| 323 |
|
return 0; |
| 324 |
|
} |
| 325 |
|
|
| 326 |
/* |
/* |
| 327 |
// turn bitmap into white canvas |
// turn bitmap into white canvas |
| 328 |
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
| 347 |
perror("malloc"), exit(1); |
perror("malloc"), exit(1); |
| 348 |
|
|
| 349 |
|
|
| 350 |
// calculate segments of bitmap for worker threads/processes and start them |
// calculate bitmap segment length for workers |
| 351 |
worker_rows = YSIZE / workers; |
worker_rows = YSIZE / workers; |
| 352 |
if (VERBOSE) |
if (VERBOSE) |
| 353 |
printf("rows for each worker: %i\n", worker_rows); |
fprintf(stdout, "rows for each worker: %i\n", worker_rows); |
| 354 |
|
|
| 355 |
|
// start workers |
| 356 |
for (worker_index = 0; worker_index < workers; worker_index++) { |
for (worker_index = 0; worker_index < workers; worker_index++) { |
| 357 |
|
|
| 358 |
// debugging: just run single thread |
// debugging: just run single thread |
| 366 |
if (worker_index == workers - 1) { |
if (worker_index == workers - 1) { |
| 367 |
worker_rows = YSIZE - worker_startrow; |
worker_rows = YSIZE - worker_startrow; |
| 368 |
if (VERBOSE) |
if (VERBOSE) |
| 369 |
printf("rows for last worker: %i\n", worker_rows); |
fprintf(stdout, "rows for last worker: %i\n", worker_rows); |
| 370 |
} |
} |
| 371 |
|
|
| 372 |
|
// assign each worker's arguments |
| 373 |
worker_args[worker_index].start_row = worker_startrow; |
worker_args[worker_index].start_row = worker_startrow; |
| 374 |
worker_args[worker_index].number_of_rows = worker_rows; |
worker_args[worker_index].number_of_rows = worker_rows; |
| 375 |
worker_args[worker_index].pBitmap = pDataBitmap; |
worker_args[worker_index].pBitmap = pDataBitmap; |
| 376 |
|
|
| 377 |
worker_handles[worker_index] = CreateThread( |
if (!use_processes) { |
| 378 |
NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes, |
|
| 379 |
0, // SIZE_T dwStackSize, |
worker_handles[worker_index] = CreateThread( |
| 380 |
&fractal_thread, // LPTHREAD_START_ROUTINE lpStartAddress, |
NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes, |
| 381 |
&worker_args[worker_index], // LPVOID lpParameter, |
0, // SIZE_T dwStackSize, |
| 382 |
0, // DWORD dwCreationFlags, |
&fractal_create_segment, // LPTHREAD_START_ROUTINE lpStartAddress, |
| 383 |
NULL // LPDWORD lpThreadId |
&worker_args[worker_index], // LPVOID lpParameter, |
| 384 |
); |
0, // DWORD dwCreationFlags, |
| 385 |
|
NULL // LPDWORD lpThreadId |
| 386 |
|
); |
| 387 |
|
|
| 388 |
|
if (!worker_handles[worker_index]) |
| 389 |
|
printErrorAndExit("CreateThread failed", GetLastError()); |
| 390 |
|
|
| 391 |
|
} else { |
| 392 |
|
|
| 393 |
|
_snprintf(szCmdline, 1023, "%s %s %i %i", argv[0], "--worker", worker_startrow, worker_rows); |
| 394 |
|
if (VERBOSE) |
| 395 |
|
fprintf(stdout, "starting worker process: %s\n", szCmdline); |
| 396 |
|
ZeroMemory( &si, sizeof(si) ); |
| 397 |
|
si.cb = sizeof(si); |
| 398 |
|
ZeroMemory( &pi, sizeof(pi) ); |
| 399 |
|
|
| 400 |
|
if (!CreateProcess( |
| 401 |
|
NULL, // No module name (use command line) |
| 402 |
|
szCmdline, // Command line |
| 403 |
|
NULL, // Process handle not inheritable |
| 404 |
|
NULL, // Thread handle not inheritable |
| 405 |
|
FALSE, // Set handle inheritance to FALSE |
| 406 |
|
0, // No creation flags |
| 407 |
|
NULL, // Use parent's environment block |
| 408 |
|
NULL, // Use parent's starting directory |
| 409 |
|
&si, // Pointer to STARTUPINFO structure |
| 410 |
|
&pi // Pointer to PROCESS_INFORMATION structure |
| 411 |
|
)) |
| 412 |
|
printErrorAndExit("CreateProcess failed", GetLastError()); |
| 413 |
|
|
| 414 |
|
worker_handles[worker_index] = pi.hProcess; |
| 415 |
|
|
| 416 |
|
} |
| 417 |
|
|
| 418 |
} |
} |
| 419 |
|
|
| 420 |
// wait for all threads |
// wait for all threads |
| 421 |
if (VERBOSE) |
if (VERBOSE) |
| 422 |
printf("waiting...\n"); |
fprintf(stdout, "waiting for workers to finish...\n"); |
| 423 |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
| 424 |
perror("WaitForMultipleObjects"); |
perror("WaitForMultipleObjects"); |
| 425 |
|
|
| 431 |
for (worker_index = 0; worker_index < workers; worker_index++) |
for (worker_index = 0; worker_index < workers; worker_index++) |
| 432 |
CloseHandle(worker_handles[worker_index]); |
CloseHandle(worker_handles[worker_index]); |
| 433 |
|
|
| 434 |
/* write the result into the file */ |
// write the result into the file |
| 435 |
if (!FlushViewOfFile(pData, 0)) { |
if (!FlushViewOfFile(pData, 0)) { |
| 436 |
err = GetLastError(); |
err = GetLastError(); |
| 437 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
| 438 |
} |
} |
| 439 |
|
|
| 440 |
/* remove the mapped file */ |
// remove the mapped file |
| 441 |
if (!UnmapViewOfFile(pData)) { |
if (!UnmapViewOfFile(pData)) { |
| 442 |
err = GetLastError(); |
err = GetLastError(); |
| 443 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
| 444 |
exit(err); |
exit(err); |
| 445 |
} |
} |
| 446 |
|
|
| 447 |
/* cleanup handles */ |
// cleanup handles |
| 448 |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
| 449 |
err = GetLastError(); |
err = GetLastError(); |
| 450 |
printErrorAndExit("Error at CloseHandle", err); |
printErrorAndExit("Error at CloseHandle", err); |