| 15 |
#define YSIZE 500 |
#define YSIZE 500 |
| 16 |
#include "algorithm.h" |
#include "algorithm.h" |
| 17 |
|
|
| 18 |
#define VERBOSE_HANDLE "stderr"; |
#define MAX_COMMAND_LINE 65536 |
| 19 |
|
#define MMAP_NAME "bmp_fractal" |
| 20 |
|
|
| 21 |
BOOL VERBOSE = FALSE; |
BOOL VERBOSE = FALSE; |
| 22 |
|
|
| 52 |
0, |
0, |
| 53 |
NULL )) |
NULL )) |
| 54 |
{ |
{ |
| 55 |
fprintf(stdout, "%s: %s\n", msg, lpMsgBuf); |
fprintf(stderr, "%s: %s\n", msg, lpMsgBuf); |
| 56 |
LocalFree(lpMsgBuf); |
LocalFree(lpMsgBuf); |
| 57 |
} |
} |
| 58 |
else |
else |
| 59 |
{ |
{ |
| 60 |
fprintf(stdout, "Error at FormatMesage: %d\n",err=GetLastError()); |
fprintf(stderr, "Error at FormatMesage: %d\n",err=GetLastError()); |
| 61 |
} |
} |
| 62 |
exit(err); |
exit(err); |
| 63 |
} |
} |
| 71 |
// open file handle |
// open file handle |
| 72 |
fd = fopen(filename, "wb+"); |
fd = fopen(filename, "wb+"); |
| 73 |
if (NULL == fd) { |
if (NULL == fd) { |
| 74 |
perror("open"); |
perror("Error while opening file for writing"); |
| 75 |
exit(1); |
exit(1); |
| 76 |
} |
} |
| 77 |
|
|
| 80 |
|
|
| 81 |
// error checking |
// error checking |
| 82 |
if (-1 == len || len != sizeof(header)) { |
if (-1 == len || len != sizeof(header)) { |
| 83 |
perror("write"); |
perror("Error while writing header to file"); |
| 84 |
exit(2); |
exit(2); |
| 85 |
} |
} |
| 86 |
|
|
| 89 |
for (i = 0; i < img_size; i++) { |
for (i = 0; i < img_size; i++) { |
| 90 |
len = fwrite("\0\0\0", 1, 3, fd); |
len = fwrite("\0\0\0", 1, 3, fd); |
| 91 |
if (-1 == len || len != 3) { |
if (-1 == len || len != 3) { |
| 92 |
perror("write"); |
perror("Error while writing data to file"); |
| 93 |
exit(4); |
exit(4); |
| 94 |
} |
} |
| 95 |
} |
} |
| 96 |
|
|
| 97 |
// close file handle |
// close file handle |
| 98 |
fclose(fd); |
if (fclose(fd) != 0) { |
| 99 |
|
perror("Error while closing file handle"); |
| 100 |
|
exit(1); |
| 101 |
|
} |
| 102 |
} |
} |
| 103 |
|
|
| 104 |
|
|
| 121 |
int x, y; |
int x, y; |
| 122 |
char bgr[3]; |
char bgr[3]; |
| 123 |
|
|
|
thread_id = GetCurrentThreadId(); |
|
|
|
|
| 124 |
// get worker arguments |
// get worker arguments |
| 125 |
args = (PWORKERARGS)lpParam; |
args = (PWORKERARGS)lpParam; |
| 126 |
|
|
| 129 |
|
|
| 130 |
// debugging |
// debugging |
| 131 |
if (VERBOSE) { |
if (VERBOSE) { |
| 132 |
|
thread_id = GetCurrentThreadId(); |
| 133 |
fprintf(stdout, "----------------------------------------------\n"); |
fprintf(stdout, "----------------------------------------------\n"); |
| 134 |
fprintf(stdout, "thread_id: %i\n", thread_id); |
fprintf(stdout, "thread_id: %i\n", thread_id); |
| 135 |
fprintf(stdout, "arg.start_row: %i\n", args->start_row); |
fprintf(stdout, "arg.start_row: %i\n", args->start_row); |
| 141 |
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--) { |
| 142 |
//fprintf(stdout, "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); |
| 143 |
for (x = 0; x < XSIZE; x++) { |
for (x = 0; x < XSIZE; x++) { |
| 144 |
|
|
| 145 |
|
// call to function in static linked library |
| 146 |
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]); |
| 147 |
|
|
| 148 |
// transfer color values to current pixel |
// transfer color values to current pixel |
| 163 |
|
|
| 164 |
} |
} |
| 165 |
|
|
| 166 |
|
BOOL scan_argv(int argc, char *argv[], char opt_name[], char *opt_value) { |
| 167 |
|
int i; |
| 168 |
|
char * opt_current_name; |
| 169 |
|
char * opt_current_value; |
| 170 |
|
|
| 171 |
|
//printf("searching for: '%s'\n", opt_name); |
| 172 |
|
|
| 173 |
|
for (i = 1; i < argc; i++) { |
| 174 |
|
opt_current_name = argv[i]; |
| 175 |
|
if (strcmp(opt_current_name, opt_name) == 0) { |
| 176 |
|
opt_current_value = argv[i+1]; |
| 177 |
|
if (opt_current_value != NULL) { |
| 178 |
|
strcpy(opt_value, opt_current_value); |
| 179 |
|
} |
| 180 |
|
return TRUE; |
| 181 |
|
} |
| 182 |
|
} |
| 183 |
|
return FALSE; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
int main(int argc, char *argv[]) { |
int main(int argc, char *argv[]) { |
| 188 |
|
|
| 193 |
unsigned char *pDataBitmap; |
unsigned char *pDataBitmap; |
| 194 |
|
|
| 195 |
// workers |
// workers |
| 196 |
int workers = 5; |
int workers; |
| 197 |
int worker_index, worker_rows, worker_startrow; |
int worker_index, worker_rows, worker_startrow; |
| 198 |
HANDLE *worker_handles; |
HANDLE *worker_handles; |
| 199 |
PWORKERARGS worker_args; |
PWORKERARGS worker_args; |
| 200 |
|
int worker_count; |
| 201 |
|
|
| 202 |
// threads or processes? |
// threads or processes? |
| 203 |
BOOL use_processes = TRUE; |
BOOL use_processes = FALSE; |
| 204 |
BOOL is_worker = FALSE; |
BOOL is_worker_process = FALSE; |
| 205 |
|
|
| 206 |
// information for creating processes |
// information for creating processes |
| 207 |
STARTUPINFO si; |
STARTUPINFO si; |
| 208 |
PROCESS_INFORMATION pi; |
PROCESS_INFORMATION pi; |
| 209 |
char szCmdline[1024]; |
char szCmdline[MAX_COMMAND_LINE]; |
| 210 |
|
|
| 211 |
|
// command line stuff |
| 212 |
|
char arg_option[1024]; |
| 213 |
|
char arg_value[1024]; |
| 214 |
|
char *bmp_filename; |
| 215 |
|
char *verbose_option = ""; |
| 216 |
|
|
| 217 |
VERBOSE = TRUE; |
|
| 218 |
|
// check command line arguments |
| 219 |
|
if (argc < 2) { |
| 220 |
|
fprintf(stderr, "Can not run without arguments!\nPlease specify '-t {number of threads}' or '-p {number of processes}' and an image filename.\n"); |
| 221 |
|
exit(EXIT_FAILURE); |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
|
| 225 |
|
// parse command line arguments |
| 226 |
|
if (scan_argv(argc, argv, "--verbose", arg_value)) { |
| 227 |
|
VERBOSE = TRUE; |
| 228 |
|
} |
| 229 |
|
|
| 230 |
// "parse" command line arguments |
if (scan_argv(argc, argv, "--worker", arg_value)) { |
| 231 |
if (argc >= 2) { |
use_processes = TRUE; |
| 232 |
if (strcmp(argv[1], "--worker") == 0) { |
is_worker_process = TRUE; |
| 233 |
is_worker = TRUE; |
|
| 234 |
|
} else if (scan_argv(argc, argv, "-p", arg_value)) { |
| 235 |
|
if (strlen(arg_value) == 0) { |
| 236 |
|
fprintf(stderr, "Please specify number of processes!\n"); |
| 237 |
|
exit(EXIT_FAILURE); |
| 238 |
|
} |
| 239 |
|
use_processes = TRUE; |
| 240 |
|
is_worker_process = FALSE; |
| 241 |
|
workers = atoi(arg_value); |
| 242 |
|
|
| 243 |
|
} else if (scan_argv(argc, argv, "-t", arg_value)) { |
| 244 |
|
if (strlen(arg_value) == 0) { |
| 245 |
|
fprintf(stderr, "Please specify number of threads!\n"); |
| 246 |
|
exit(EXIT_FAILURE); |
| 247 |
} |
} |
| 248 |
|
use_processes = FALSE; |
| 249 |
|
is_worker_process = FALSE; |
| 250 |
|
workers = atoi(arg_value); |
| 251 |
|
|
| 252 |
} |
} |
| 253 |
|
|
| 254 |
|
|
| 255 |
if (VERBOSE && use_processes) { |
if (VERBOSE && use_processes) { |
| 256 |
fprintf(stdout, "===================================================== "); |
fprintf(stdout, "===================================================== "); |
| 257 |
if (is_worker) |
if (is_worker_process) |
| 258 |
fprintf(stdout, "WORKER-PROCESS\n"); |
fprintf(stdout, "WORKER-PROCESS\n"); |
| 259 |
else |
else |
| 260 |
fprintf(stdout, "MASTER-PROCESS\n"); |
fprintf(stdout, "MASTER-PROCESS\n"); |
| 261 |
} |
} |
|
|
|
|
// create empty bmp-file (black background) |
|
|
if (!is_worker) |
|
|
write_blank_file("test.bmp"); |
|
| 262 |
|
|
| 263 |
if (!is_worker) { |
|
| 264 |
|
// master creates memory mapped file ("empty" image) |
| 265 |
|
if (!is_worker_process) { |
| 266 |
|
|
| 267 |
/* open file for reading and writing */ |
if (argc < 4) { |
| 268 |
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
fprintf(stderr, "Must give filename of image as third argument!\n"); |
| 269 |
|
exit(EXIT_FAILURE); |
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
bmp_filename = argv[3]; |
| 273 |
|
|
| 274 |
|
// create empty bmp-file (black background) |
| 275 |
|
write_blank_file(bmp_filename); |
| 276 |
|
|
| 277 |
|
// open file for reading and writing |
| 278 |
|
hFile = CreateFile(bmp_filename, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 279 |
if (INVALID_HANDLE_VALUE == hFile) { |
if (INVALID_HANDLE_VALUE == hFile) { |
| 280 |
err = GetLastError(); |
err = GetLastError(); |
| 281 |
printErrorAndExit("Error at CreateFile",err); |
printErrorAndExit("Error at CreateFile",err); |
| 282 |
} |
} |
| 283 |
|
|
| 284 |
/* create the file mapping object */ |
// create the file mapping object |
| 285 |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, MMAP_NAME); |
| 286 |
if (NULL == hMap) { |
if (NULL == hMap) { |
| 287 |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
| 288 |
} |
} |
| 289 |
|
|
| 290 |
|
// worker uses existing memory mapped file |
| 291 |
} else { |
} else { |
| 292 |
|
|
| 293 |
// open existing mapping object |
// open existing mapping object |
| 294 |
hMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, "bmp_fractal"); |
hMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, MMAP_NAME); |
| 295 |
if (NULL == hMap) |
if (NULL == hMap) |
| 296 |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
| 297 |
} |
} |
| 298 |
|
|
| 299 |
/* map the whole file into the process context */ |
// map the whole file into the process context |
| 300 |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
| 301 |
if (NULL == pData) { |
if (NULL == pData) { |
| 302 |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
| 313 |
} |
} |
| 314 |
|
|
| 315 |
|
|
| 316 |
if (use_processes && is_worker) { |
if (use_processes && is_worker_process) { |
| 317 |
|
|
| 318 |
if (VERBOSE) |
if (VERBOSE) |
| 319 |
fprintf(stdout, "inside worker-process\n"); |
fprintf(stdout, "inside worker-process\n"); |
| 324 |
|
|
| 325 |
// allocate memory for one worker's arguments |
// allocate memory for one worker's arguments |
| 326 |
if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL) |
if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL) |
| 327 |
perror("malloc"), exit(1); |
perror("Error while allocating memory for worker arguments via malloc"), exit(1); |
| 328 |
|
|
| 329 |
// assign worker's arguments |
// assign worker's arguments |
| 330 |
worker_args[0].start_row = worker_startrow; |
worker_args[0].start_row = worker_startrow; |
| 332 |
worker_args[0].pBitmap = pDataBitmap; |
worker_args[0].pBitmap = pDataBitmap; |
| 333 |
|
|
| 334 |
fractal_create_segment(&worker_args[0]); |
fractal_create_segment(&worker_args[0]); |
| 335 |
|
|
| 336 |
|
// cleanup mmap-handle |
| 337 |
|
if (!CloseHandle(hMap)) |
| 338 |
|
printErrorAndExit("Error at CloseHandle", GetLastError()); |
| 339 |
|
|
| 340 |
return 0; |
return 0; |
| 341 |
} |
} |
| 342 |
|
|
| 343 |
/* |
/* |
| 357 |
|
|
| 358 |
// allocate memory for table of all worker handles |
// allocate memory for table of all worker handles |
| 359 |
if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL) |
if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL) |
| 360 |
perror("malloc"), exit(1); |
perror("Error while allocating memory for worker handles via malloc"), exit(1); |
| 361 |
|
|
| 362 |
// allocate memory for table of all worker arguments |
// allocate memory for table of all worker arguments |
| 363 |
if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL) |
if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL) |
| 364 |
perror("malloc"), exit(1); |
perror("Error while allocating memory for worker arguments via malloc"), exit(1); |
| 365 |
|
|
| 366 |
|
|
| 367 |
// calculate bitmap segment length for workers |
// calculate bitmap segment length for workers |
| 407 |
|
|
| 408 |
} else { |
} else { |
| 409 |
|
|
| 410 |
_snprintf(szCmdline, 1023, "%s %s %i %i", argv[0], "--worker", worker_startrow, worker_rows); |
if (VERBOSE) |
| 411 |
|
verbose_option = "--verbose"; |
| 412 |
|
_snprintf(szCmdline, MAX_COMMAND_LINE - 1, "%s %s %i %i %s", argv[0], "--worker", worker_startrow, worker_rows, verbose_option); |
| 413 |
if (VERBOSE) |
if (VERBOSE) |
| 414 |
fprintf(stdout, "starting worker process: %s\n", szCmdline); |
fprintf(stdout, "starting worker process: %s\n", szCmdline); |
| 415 |
|
|
| 416 |
|
// prepare data structures for CreateProcess |
| 417 |
ZeroMemory( &si, sizeof(si) ); |
ZeroMemory( &si, sizeof(si) ); |
| 418 |
si.cb = sizeof(si); |
si.cb = sizeof(si); |
| 419 |
ZeroMemory( &pi, sizeof(pi) ); |
ZeroMemory( &pi, sizeof(pi) ); |
| 438 |
|
|
| 439 |
} |
} |
| 440 |
|
|
|
// wait for all threads |
|
| 441 |
if (VERBOSE) |
if (VERBOSE) |
| 442 |
fprintf(stdout, "waiting for workers to finish...\n"); |
fprintf(stdout, "waiting for workers to finish...\n"); |
| 443 |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
|
| 444 |
perror("WaitForMultipleObjects"); |
// wait for all workers |
| 445 |
|
for (worker_index = 0; worker_index < workers; worker_index += MAXIMUM_WAIT_OBJECTS) { |
| 446 |
|
worker_count = ((workers - worker_index) > MAXIMUM_WAIT_OBJECTS) ? MAXIMUM_WAIT_OBJECTS : (workers - worker_index); |
| 447 |
|
if (WaitForMultipleObjects(worker_count, &worker_handles[worker_index], TRUE, INFINITE) == WAIT_FAILED) |
| 448 |
|
printErrorAndExit("Error at WaitForMultipleObjects", GetLastError()); |
| 449 |
|
} |
| 450 |
|
|
| 451 |
// debugging: just run single thread |
// debugging: just run single thread |
| 452 |
//if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) |
//if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) |
| 453 |
// perror("WaitForSingleObject"); |
// perror("WaitForSingleObject"); |
| 454 |
|
|
| 455 |
// close all worker handles |
// close all worker handles |
| 456 |
for (worker_index = 0; worker_index < workers; worker_index++) |
for (worker_index = 0; worker_index < workers; worker_index++) { |
| 457 |
CloseHandle(worker_handles[worker_index]); |
if (!CloseHandle(worker_handles[worker_index])) |
| 458 |
|
printErrorAndExit("Error at CloseHandle for worker handles", GetLastError()); |
|
/* write the result into the file */ |
|
|
if (!FlushViewOfFile(pData, 0)) { |
|
|
err = GetLastError(); |
|
|
printErrorAndExit("Error at UnmapViewOfFile", err); |
|
|
} |
|
|
|
|
|
/* remove the mapped file */ |
|
|
if (!UnmapViewOfFile(pData)) { |
|
|
err = GetLastError(); |
|
|
printErrorAndExit("Error at UnmapViewOfFile", err); |
|
|
exit(err); |
|
|
} |
|
|
|
|
|
/* cleanup handles */ |
|
|
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
|
|
err = GetLastError(); |
|
|
printErrorAndExit("Error at CloseHandle", err); |
|
| 459 |
} |
} |
| 460 |
|
|
| 461 |
|
// write the result into the file |
| 462 |
|
if (!FlushViewOfFile(pData, 0)) |
| 463 |
|
printErrorAndExit("Error at UnmapViewOfFile", GetLastError()); |
| 464 |
|
|
| 465 |
|
// remove the mapped file |
| 466 |
|
if (!UnmapViewOfFile(pData)) |
| 467 |
|
printErrorAndExit("Error at UnmapViewOfFile", GetLastError()); |
| 468 |
|
|
| 469 |
|
// cleanup handles |
| 470 |
|
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) |
| 471 |
|
printErrorAndExit("Error at CloseHandle for memory mapped file", GetLastError()); |
| 472 |
|
|
| 473 |
free(worker_args); |
free(worker_args); |
| 474 |
free(worker_handles); |
free(worker_handles); |