| 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(stderr, "%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(stderr, "Error at FormatMesage: %d\n",err=GetLastError()); |
| 58 |
} |
} |
| 59 |
exit(err); |
exit(err); |
| 60 |
} |
} |
| 68 |
// open file handle |
// open file handle |
| 69 |
fd = fopen(filename, "wb+"); |
fd = fopen(filename, "wb+"); |
| 70 |
if (NULL == fd) { |
if (NULL == fd) { |
| 71 |
perror("open"); |
perror("Error while opening file for writing"); |
| 72 |
exit(1); |
exit(1); |
| 73 |
} |
} |
| 74 |
|
|
| 77 |
|
|
| 78 |
// error checking |
// error checking |
| 79 |
if (-1 == len || len != sizeof(header)) { |
if (-1 == len || len != sizeof(header)) { |
| 80 |
perror("write"); |
perror("Error while writing header to file"); |
| 81 |
exit(2); |
exit(2); |
| 82 |
} |
} |
| 83 |
|
|
| 86 |
for (i = 0; i < img_size; i++) { |
for (i = 0; i < img_size; i++) { |
| 87 |
len = fwrite("\0\0\0", 1, 3, fd); |
len = fwrite("\0\0\0", 1, 3, fd); |
| 88 |
if (-1 == len || len != 3) { |
if (-1 == len || len != 3) { |
| 89 |
perror("write"); |
perror("Error while writing data to file"); |
| 90 |
exit(4); |
exit(4); |
| 91 |
} |
} |
| 92 |
} |
} |
| 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 |
|
BOOL scan_argv(int argc, char *argv[], char opt_name[], char *opt_value) { |
| 161 |
|
int i; |
| 162 |
|
char * opt_current_name; |
| 163 |
|
char * opt_current_value; |
| 164 |
|
|
| 165 |
|
//printf("searching for: '%s'\n", opt_name); |
| 166 |
|
|
| 167 |
|
for (i = 1; i < argc; i++) { |
| 168 |
|
opt_current_name = argv[i]; |
| 169 |
|
if (strcmp(opt_current_name, opt_name) == 0) { |
| 170 |
|
opt_current_value = argv[i+1]; |
| 171 |
|
if (opt_current_value != NULL) { |
| 172 |
|
strcpy(opt_value, opt_current_value); |
| 173 |
|
} |
| 174 |
|
return TRUE; |
| 175 |
|
} |
| 176 |
|
} |
| 177 |
|
return FALSE; |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
|
| 181 |
int main(int argc, char *argv[]) { |
int main(int argc, char *argv[]) { |
| 182 |
|
|
| 193 |
PWORKERARGS worker_args; |
PWORKERARGS worker_args; |
| 194 |
|
|
| 195 |
// threads or processes? |
// threads or processes? |
| 196 |
BOOL use_processes = TRUE; |
BOOL use_processes = FALSE; |
| 197 |
int worker_id = MASTER; |
BOOL is_worker_process = FALSE; |
| 198 |
|
|
| 199 |
// information for creating processes |
// information for creating processes |
| 200 |
STARTUPINFO si; |
STARTUPINFO si; |
| 201 |
PROCESS_INFORMATION pi; |
PROCESS_INFORMATION pi; |
| 202 |
char szCmdline[1024]; |
char szCmdline[65536]; |
| 203 |
|
|
| 204 |
|
// command line stuff |
| 205 |
|
char arg_option[1024]; |
| 206 |
|
char arg_value[1024]; |
| 207 |
|
char *bmp_filename; |
| 208 |
|
char *verbose_option = ""; |
| 209 |
|
|
| 210 |
// "parse" command line arguments |
|
| 211 |
if (argc >= 2) { |
// parse command line arguments |
| 212 |
worker_id = atoi(argv[1]); |
if (argc < 2) { |
| 213 |
|
fprintf(stderr, "Can not run without arguments!\nPlease specify '-t {number of threads}' or '-p {number of processes}' and an image filename.\n"); |
| 214 |
|
exit(EXIT_FAILURE); |
| 215 |
} |
} |
| 216 |
|
|
| 217 |
if (VERBOSE) { |
if (scan_argv(argc, argv, "--verbose", arg_value)) { |
| 218 |
printf("==============================================================\n"); |
VERBOSE = TRUE; |
|
printf("worker-id: %i\n", worker_id); |
|
| 219 |
} |
} |
| 220 |
|
|
| 221 |
// create empty bmp-file (black background) |
if (scan_argv(argc, argv, "--worker", arg_value)) { |
| 222 |
if (worker_id == MASTER) |
use_processes = TRUE; |
| 223 |
write_blank_file("test.bmp"); |
is_worker_process = TRUE; |
| 224 |
|
|
| 225 |
|
} else if (scan_argv(argc, argv, "-p", arg_value)) { |
| 226 |
|
if (strlen(arg_value) == 0) { |
| 227 |
|
fprintf(stderr, "Please specify number of processes!\n"); |
| 228 |
|
exit(EXIT_FAILURE); |
| 229 |
|
} |
| 230 |
|
use_processes = TRUE; |
| 231 |
|
is_worker_process = FALSE; |
| 232 |
|
workers = atoi(arg_value); |
| 233 |
|
|
| 234 |
|
} else if (scan_argv(argc, argv, "-t", arg_value)) { |
| 235 |
|
if (strlen(arg_value) == 0) { |
| 236 |
|
fprintf(stderr, "Please specify number of threads!\n"); |
| 237 |
|
exit(EXIT_FAILURE); |
| 238 |
|
} |
| 239 |
|
use_processes = FALSE; |
| 240 |
|
is_worker_process = FALSE; |
| 241 |
|
workers = atoi(arg_value); |
| 242 |
|
|
| 243 |
|
} |
| 244 |
|
|
| 245 |
|
|
| 246 |
|
if (VERBOSE && use_processes) { |
| 247 |
|
fprintf(stdout, "===================================================== "); |
| 248 |
|
if (is_worker_process) |
| 249 |
|
fprintf(stdout, "WORKER-PROCESS\n"); |
| 250 |
|
else |
| 251 |
|
fprintf(stdout, "MASTER-PROCESS\n"); |
| 252 |
|
} |
| 253 |
|
|
| 254 |
if (worker_id == MASTER) { |
|
| 255 |
|
// master creates memory mapped file ("empty" image) |
| 256 |
|
if (!is_worker_process) { |
| 257 |
|
|
| 258 |
|
if (argc < 4) { |
| 259 |
|
fprintf(stderr, "Must give filename of image as third argument!\n"); |
| 260 |
|
exit(EXIT_FAILURE); |
| 261 |
|
} |
| 262 |
|
|
| 263 |
/* open file for reading and writing */ |
bmp_filename = argv[3]; |
| 264 |
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
|
| 265 |
|
// create empty bmp-file (black background) |
| 266 |
|
write_blank_file(bmp_filename); |
| 267 |
|
|
| 268 |
|
// open file for reading and writing |
| 269 |
|
hFile = CreateFile(bmp_filename, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 270 |
if (INVALID_HANDLE_VALUE == hFile) { |
if (INVALID_HANDLE_VALUE == hFile) { |
| 271 |
err = GetLastError(); |
err = GetLastError(); |
| 272 |
printErrorAndExit("Error at CreateFile",err); |
printErrorAndExit("Error at CreateFile",err); |
| 273 |
} |
} |
| 274 |
|
|
| 275 |
/* create the file mapping object */ |
// create the file mapping object |
| 276 |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
| 277 |
if (NULL == hMap) { |
if (NULL == hMap) { |
| 278 |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
| 279 |
} |
} |
| 280 |
|
|
| 281 |
|
// worker uses existing memory mapped file |
| 282 |
} else { |
} else { |
| 283 |
|
|
| 284 |
// open existing mapping object |
// open existing mapping object |
| 287 |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
| 288 |
} |
} |
| 289 |
|
|
| 290 |
/* map the whole file into the process context */ |
// map the whole file into the process context |
| 291 |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
| 292 |
if (NULL == pData) { |
if (NULL == pData) { |
| 293 |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
| 299 |
|
|
| 300 |
// debugging |
// debugging |
| 301 |
if (VERBOSE) { |
if (VERBOSE) { |
| 302 |
printf("pos. of file: %p\n", pData); |
fprintf(stdout, "pos. of file: %p\n", pData); |
| 303 |
printf("pos. of bitmap: %p\n", pDataBitmap); |
fprintf(stdout, "pos. of bitmap: %p\n", pDataBitmap); |
| 304 |
} |
} |
| 305 |
|
|
| 306 |
|
|
| 307 |
if (use_processes && worker_id != MASTER) { |
if (use_processes && is_worker_process) { |
| 308 |
|
|
| 309 |
if (VERBOSE) |
if (VERBOSE) |
| 310 |
printf("inside worker-process\n"); |
fprintf(stdout, "inside worker-process\n"); |
|
|
|
|
worker_index = worker_id; |
|
| 311 |
|
|
| 312 |
|
// get segment information from command line |
| 313 |
worker_startrow = atoi(argv[2]); |
worker_startrow = atoi(argv[2]); |
| 314 |
worker_rows = atoi(argv[3]); |
worker_rows = atoi(argv[3]); |
| 315 |
|
|
| 316 |
// allocate memory for one worker's arguments |
// allocate memory for one worker's arguments |
| 317 |
if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL) |
if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL) |
| 318 |
perror("malloc"), exit(1); |
perror("Error while allocating memory for worker arguments via malloc"), exit(1); |
| 319 |
|
|
| 320 |
// assign worker's arguments |
// assign worker's arguments |
| 321 |
worker_args[0].start_row = worker_startrow; |
worker_args[0].start_row = worker_startrow; |
| 323 |
worker_args[0].pBitmap = pDataBitmap; |
worker_args[0].pBitmap = pDataBitmap; |
| 324 |
|
|
| 325 |
fractal_create_segment(&worker_args[0]); |
fractal_create_segment(&worker_args[0]); |
| 326 |
|
|
| 327 |
|
// cleanup mmap-handle |
| 328 |
|
if (!CloseHandle(hMap)) |
| 329 |
|
printErrorAndExit("Error at CloseHandle", GetLastError()); |
| 330 |
|
|
| 331 |
return 0; |
return 0; |
| 332 |
} |
} |
| 333 |
|
|
| 334 |
/* |
/* |
| 348 |
|
|
| 349 |
// allocate memory for table of all worker handles |
// allocate memory for table of all worker handles |
| 350 |
if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL) |
if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL) |
| 351 |
perror("malloc"), exit(1); |
perror("Error while allocating memory for worker handles via malloc"), exit(1); |
| 352 |
|
|
| 353 |
// allocate memory for table of all worker arguments |
// allocate memory for table of all worker arguments |
| 354 |
if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL) |
if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL) |
| 355 |
perror("malloc"), exit(1); |
perror("Error while allocating memory for worker arguments via malloc"), exit(1); |
| 356 |
|
|
| 357 |
|
|
| 358 |
// calculate bitmap segment length for workers |
// calculate bitmap segment length for workers |
| 359 |
worker_rows = YSIZE / workers; |
worker_rows = YSIZE / workers; |
| 360 |
if (VERBOSE) |
if (VERBOSE) |
| 361 |
printf("rows for each worker: %i\n", worker_rows); |
fprintf(stdout, "rows for each worker: %i\n", worker_rows); |
| 362 |
|
|
| 363 |
// start workers |
// start workers |
| 364 |
for (worker_index = 0; worker_index < workers; worker_index++) { |
for (worker_index = 0; worker_index < workers; worker_index++) { |
| 374 |
if (worker_index == workers - 1) { |
if (worker_index == workers - 1) { |
| 375 |
worker_rows = YSIZE - worker_startrow; |
worker_rows = YSIZE - worker_startrow; |
| 376 |
if (VERBOSE) |
if (VERBOSE) |
| 377 |
printf("rows for last worker: %i\n", worker_rows); |
fprintf(stdout, "rows for last worker: %i\n", worker_rows); |
| 378 |
} |
} |
| 379 |
|
|
| 380 |
// assign each worker's arguments |
// assign each worker's arguments |
| 398 |
|
|
| 399 |
} else { |
} else { |
| 400 |
|
|
| 401 |
_snprintf(szCmdline, 1023, "%s %i %i %i", argv[0], worker_index + 1, worker_startrow, worker_rows); |
if (VERBOSE) |
| 402 |
|
verbose_option = "--verbose"; |
| 403 |
|
_snprintf(szCmdline, 1023, "%s %s %i %i %s", argv[0], "--worker", worker_startrow, worker_rows, verbose_option); |
| 404 |
|
if (VERBOSE) |
| 405 |
|
fprintf(stdout, "starting worker process: %s\n", szCmdline); |
| 406 |
ZeroMemory( &si, sizeof(si) ); |
ZeroMemory( &si, sizeof(si) ); |
| 407 |
si.cb = sizeof(si); |
si.cb = sizeof(si); |
| 408 |
ZeroMemory( &pi, sizeof(pi) ); |
ZeroMemory( &pi, sizeof(pi) ); |
| 429 |
|
|
| 430 |
// wait for all threads |
// wait for all threads |
| 431 |
if (VERBOSE) |
if (VERBOSE) |
| 432 |
printf("waiting for workers to finish...\n"); |
fprintf(stdout, "waiting for workers to finish...\n"); |
| 433 |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
| 434 |
perror("WaitForMultipleObjects"); |
printErrorAndExit("Error at WaitForMultipleObjects", GetLastError()); |
| 435 |
|
|
| 436 |
// debugging: just run single thread |
// debugging: just run single thread |
| 437 |
//if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) |
//if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) |
| 441 |
for (worker_index = 0; worker_index < workers; worker_index++) |
for (worker_index = 0; worker_index < workers; worker_index++) |
| 442 |
CloseHandle(worker_handles[worker_index]); |
CloseHandle(worker_handles[worker_index]); |
| 443 |
|
|
| 444 |
/* write the result into the file */ |
// write the result into the file |
| 445 |
if (!FlushViewOfFile(pData, 0)) { |
if (!FlushViewOfFile(pData, 0)) { |
| 446 |
err = GetLastError(); |
err = GetLastError(); |
| 447 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
| 448 |
} |
} |
| 449 |
|
|
| 450 |
/* remove the mapped file */ |
// remove the mapped file |
| 451 |
if (!UnmapViewOfFile(pData)) { |
if (!UnmapViewOfFile(pData)) { |
| 452 |
err = GetLastError(); |
err = GetLastError(); |
| 453 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
| 454 |
exit(err); |
exit(err); |
| 455 |
} |
} |
| 456 |
|
|
| 457 |
/* cleanup handles */ |
// cleanup handles |
| 458 |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
| 459 |
err = GetLastError(); |
err = GetLastError(); |
| 460 |
printErrorAndExit("Error at CloseHandle", err); |
printErrorAndExit("Error at CloseHandle", err); |